4

In the theme admin menu below, the "MyTheme Menu Label" is being duplicated twice on the sidebar menu, once for the main menu link and again for the first submenu link.

How can I remove the 2nd instance of the link

add_menu_page(
    "MyTheme", 
    "MyTheme Menu Label", //THIS IS REPEATED TWICE IN THE MENU
    "edit_themes", 
    "functions.php", 
    'theme_admin', 
    get_bloginfo('template_directory') .'/img/favicon.png',31
);

add_submenu_page(
    'functions.php', 
    "SEO Options",
    "SEO",
    'edit_themes', 
    'my-seo-options', 
    'theme_admin'
);

add_submenu_page(
    'functions.php', 
    "Misc Options",
    "Misc",
    'edit_themes', 
    'my-misc-options', 
    'theme_admin'
);

 //etc...
4

There is a workaround to hide this automatically created submenu. In the past I've used it quite often, but lately I have returned to leaving it as is (or renaming it, as m0r7if3r suggested).

Also note, that aside your main question, you have switched the positions of the menu_slug and function arguments in add_menu_page, see the codex for reference.

This is how it is done anyhow:

/* Add top level menu */
add_menu_page(
    'MyTheme', 
    'MyTheme Menu Label',
    'edit_themes', 
    'theme_admin',        // menu slug
    'functions.php',        // function
    get_bloginfo('template_directory') .'/img/favicon.png',
    31
);

/* remove duplicate menu hack */
add_submenu_page(
    'theme_admin',        // parent slug, same as above menu slug
    '',        // empty page title
    '',        // empty menu title
    'edit_themes',        // same capability as above
    'theme_admin',        // same menu slug as parent slug
    'functions.php',        // same function as above
}

This isn't too clean, but afaik the only way to hide the duplicate submenu.

  • 1
    I don't have a high enough rep yet so I can't add a answer. From WordPress 3.0 and up you can use: remove_submenu_page($menuSlug, $menuSlug); were $menuSlug is the menu slug of the top level or root menu item. – Andy Braham Jul 28 '17 at 12:44
6

From the codex page on admin menus:

In situations where a plugin is creating its own top-level menu, the first submenu will normally have the same link title as the top-level menu and hence the link will be duplicated. The duplicate link title can be avoided by calling the add_submenu_page function the first time with the parent_slug and menu_slug parameters being given the same value.

Personally, I think that it would be bad practice to do anything but rename the top link, as it would break consistency with the rest of the admin bar.

  • this is the right way – Pons Jan 7 '17 at 17:51
0

There's a function that should cover this:

remove_submenu_page( $plugin_slug, $plugin_slug );

Not the answer you're looking for? Browse other questions tagged or ask your own question.