d1ch0t0my
d1ch0t0my

Reputation: 443

Add custom top level external and custom submenu external links to WP Admin

I want to be able to add a new admin menu section in the Wordpress Admin area in this format but using external links:

Top Level
  - item 1
  - item 2
  - item 3

However the functions add_menu_page() and add_submenu_page() seem to only allow the adding of menu items to existing post types or pages. What is the simplest way to achieve a menu with all external links? Many thanks.

Upvotes: 1

Views: 3907

Answers (1)

FluffyKitten
FluffyKitten

Reputation: 14312

You can't add external links directly using add_menu_page() and add_submenu_page() as you have discovered, but you can manipulate the global $menu and $submenu variables to achieve this (up to WP 4.8.1 anyway)

1. Add an external link as top-level menu

If you want to add an external link as a single top-level menu, you can do it as follows:

add_action( 'admin_menu', 'admin_menu_add_external_link_top_level' );

function admin_menu_add_external_link_top_level() {
    global $menu;

    $menu_slug = "external_slug"; // just a placeholder for when we call add_menu_page
    $menu_pos = 1; // whatever position you want your menu to appear

    // create the top level menu, using $menu_slug as a placeholder for the link
    add_menu_page( 'admin_menu_add_external_link_top_level', 'External Link', 'read', $menu_slug, '', 'icon', $menu_pos );

    // replace the slug with your external url
    $menu[$menu_pos][2] = "http://www.example.com";
}

Ref: Adding an Arbitrary Link to the Admin Menu?

The main thing to note here is that you need to pass a placeholder value for "slug" into add_menu_page, which you will then replace with your external url. If you try to pass the url in as the slug, Wordpress will append it to the website domain, e.g. http://yourdomain.com/www.example.com.

2. Add a top-level menu with a submenu of external links

Add a normal top-level menu with a submenu containing external links as follows:

add_action('admin_menu', 'admin_menu_add_external_links_as_submenu');

function admin_menu_add_external_links_as_submenu() {
    global $submenu;

    $menu_slug = "externallink"; // used as "key" in menus
    $menu_pos = 1; // whatever position you want your menu to appear

    // create the top level menu
    add_menu_page( 'external_link', 'External Links', 'read', $menu_slug, '', '', $menu_pos);

    // add the external links to the slug you used when adding the top level menu
    $submenu[$menu_slug][] = array('Example', 'manage_options', 'http://www.example.com/');
    $submenu[$menu_slug][] = array('Google', 'manage_options', 'https://www.google.com/');
}

Ref: how to add custom link on wordpress admin sidebar

In this example, we set up the top-level domain as normal using add_menu_page. However instead of using add_submenu_page for the submenu, we need to manipulate the $submenu global variable directly to add our external urls.

3. Add a top-level menu with external link and a submenu of external links

Although its not totally clear I think you are looking to add a top-level menu that is an external link and also has a sub menu of external links?

I'm not sure why you'd want to do that, but if you do, you can combine the above as follows to achieve this:

add_action( 'admin_menu', 'admin_menu_add_external_link_top_submenu' );

function admin_menu_add_external_link_top_submenu() {
    global $menu;
    global $submenu;

    $menu_slug = "http://www.example.com"; // url for the top-level option
    $menu_pos = 1; // whatever position you want your menu to appear

    // create the top level menu, using $menu_slug as a placeholder for the link
    add_menu_page( 'admin_menu_add_external_link_top_level', 'External Links', 'read', 'my_slug', '', 'icon', $menu_pos );

    // replace the slug with your external url
   $menu[$menu_pos][2] = $myslug;

    // add your submenu of external links
    // add the links to the "slug" for your top-level menu (which is now your external url)
    $submenu[$menu_slug][] = array('Example', 'manage_options', 'http://www.example.com/');
    $submenu[$menu_slug][] = array('Google', 'manage_options', 'https://www.google.com/');
}

Ref: Just me and some trial & error :)

This example is a combination of the code in options 1 and 2: Set up your top-level menu using add_menu_page; Replace the slug with your external url and finally add your submenu links directly using add_submenu_page.

Upvotes: 7

Related Questions