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

Related posts

Leave a Reply

3 comments

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

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