Blog

How To Add An External Or Customizer Link To The WordPress Admin Menu

Free Web Hosting

WordPress has a simple API for adding new pages to the admin. However, sometimes you may not want your link to be a new settings page. One very practical alternative is to directly link to the Customizer active on the section or panel with the settings for your theme or plugin. But for some reason, WordPress makes this really difficult.

One way we can achieve this is to inject our link into the $submenu global variable, as demonstrated by Mark Duncan on the WordPress Stack Exchange.

Adding a new link by changing the global variable:

https://gist.github.com/cameronjonesweb/35fca656016646dda0f1000fcdabf498#file-admin-menu-override-global-php

This works fairly reliably, however if you use the WordPress Coding Standards you’ll find that it will complain as you are discouraged from overriding WordPress globals. This method is also risky, as Ross Mckay warns, because it bypasses the API for adding menu items. If WordPress changes things in the future, there is a chance this may no longer work.

An alternative approach

So far, the only other way I’ve found to make this possible is to change the link using the clean_url filter used by the esc_url function. This function is used on each link in the admin menu to make sure the links are valid URLs. Once the link has been validated, it’s run through the clean_url filter where we can change where the admin menu link is pointing to.

The following code snippets demonstrate how you can use the clean_url filter to change where your admin menu links go to. These particular examples apply to submenu links under Settings, but can be applied to top level menu links and submenu links under any other top level menu item.

Changing the link to go to an external URL:

https://gist.github.com/cameronjonesweb/35fca656016646dda0f1000fcdabf498#file-admin-menu-external-link-php

Changing the link to go to a certain panel in the Customizer:

https://gist.github.com/cameronjonesweb/35fca656016646dda0f1000fcdabf498#file-admin-menu-customizer-link-php

This technique is a still little hacky, so if you know of a better way please let me know in the comments below.

Did you find this post useful?

YesNo