最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp admin - admin_url in plugins

programmeradmin2浏览0评论

I am trying to understand when usage of admin url is required. If I do this in my plugins page, they all do the same thing.

<a href="<?php echo admin_url("admin.php?page=fap_playlist_manager"); ?>">Back to Playlist manager</a>

<a href="admin.php?page=fap_playlist_manager">Back to Playlist manager</a>

<a href="?page=fap_playlist_manager">Back to Playlist manager</a>

What does this depends on?

I am trying to understand when usage of admin url is required. If I do this in my plugins page, they all do the same thing.

<a href="<?php echo admin_url("admin.php?page=fap_playlist_manager"); ?>">Back to Playlist manager</a>

<a href="admin.php?page=fap_playlist_manager">Back to Playlist manager</a>

<a href="?page=fap_playlist_manager">Back to Playlist manager</a>

What does this depends on?

Share Improve this question asked Apr 29, 2017 at 8:42 ToniqToniq 4476 silver badges15 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

First rule of wordpress development is to always use a core API if one is available, there is usually a good reason for its existence.

In this case, you are assuming something about how files are called and the URLs they are in. For an example were such an assumption will fail, is the (rarely ask about, but it is asked about) case of someone wanting to "prettify" the admin url and replace /wp-admin/admin.php?... with /admin/.... For this kind of functionality people will most likely override the admin_url result by using its filter, and it will not be possible to adjust your "hardcoded" URLs to fit in.

Granted, for this specific question the chance of any noticeable difference between the 3 alternative is close to zero, but it is a very good habit never to assume anything about the URL (or directory) structure when you develop a plugin or a theme that you intent to make available to everyone.

They are all the same, as you mentioned.

The first method admin_url('URL HERE') will output the admin's URL, and then appends the entered URL to it. This is the standard method, since someone might have changed the admin address through different methods.

The second one outputs the core of the admin files (which is used to connect to database, display non-core pages, etc) and then manually appends your page's URL to it.

The third one adds ?page=fap_playlist_manager to whatever the current URL is. This might cause issue on some rewrite conditions.

The best practice is to go with the core function admin_url() and then append any other URL to it, whether by using admin_url('LINK HERE') or admin_url().'/LINK HERE/'. Both are the same.

EDIT: As @mark mentioned, this admin_url().'/LINK HERE/' does not filter the LINK HERE, IF it needs to be filtered. Otherwise, no difference.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论