How to remove duplicate link from add_menu_page

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...

3 Answers
3

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.

Leave a Comment