Add an external link to the WordPress admin menu

WordPress has a good API for adding admin menu items. Unfortunately, it doesn’t allow you to specify a direct link to something, only a “slug” that links your menu item to a callback function to run when your menu item is selected. But fortunately, there’s a hack.

When you callĀ add_submenu_page() or one of the section-specific functions likeĀ add_management_page() to add a menu item, internally WordPress adds an item to a submenu array that it uses later on to build the admin menu. The array is held in a global variable, so it’s easy enough to add items directly to it. Many thanks to this answer on the WordPress Answers stackexchange, by t31os.

NB: I strongly recommend using the API to add your menu items if at all possible! The WordPress admin implementation could change in a future version, breaking code that directly accesses the submenu array. There, I said it :)

Now, if you want to see what this array looks like, this snippet will show you (on PHP >= 5.3):

add_action('admin_menu', function() {
    global $submenu;
    echo "<pre>", print_r($submenu,1), "</pre>n";
});

You’ll see that the admin menu is stored in a nested array with an associative (keyed) array for the top level menu, and numerically indexed arrays for the second level menus. The second level menus are numerically indexed arrays of title, required capability and page slug.

To add an external link into the menu, we just need to pick a top-level menu item by its key, and add a new submenu item using the external link instead of the relative page link. Here are the keys for the standard admin menu items (do a print_r() of the array to get any custom top-level items added by plugins):

index.php => Dashboard
edit.php => Posts
upload.php => Media
link-manager.php => Links
edit.php?post_type=page => Pages
edit-comments.php => Comments
themes.php => Appearance
plugins.php => Plugins
users.php => Users
tools.php => Tools
options-general.php => Settings

Here’s a simple example adding an external link to the Tools menu, accessible to anyone who can manage WordPress options (e.g. administrator):

add_action('admin_menu', 'example_admin_menu');

/**
* add external link to Tools area
*/
function example_admin_menu() {
    global $submenu;
    $url = 'http://www.example.com/';
    $submenu['tools.php'][] = array('Example', 'manage_options', $url);
}

And job is done, at least until WordPress changes its admin menu implementation :)

Facebooktwittergoogle_plusredditlinkedinmailFacebooktwittergoogle_plusredditlinkedinmail