How to remove duplicate sub-menu name for top level menu items in a plugin?

If you dont want ‘TopLevel’ menu to represent a custom page you can use:

  add_menu_page( 'TopLevel', 'TopLevel', 'MENU_CAP_LVL', 'MENU_SLUG', 'MENU_CB' );
  add_submenu_page( 'MENU_SLUG', 'SubMenu', 'SubMenu', 'MENU_CAP_LVL', 'SUB_MENU_SLUG', 'SUB_MENU_CB' );
  add_submenu_page( 'MENU_SLUG', 'SubMenu-A', 'SubMenu-A', 'MENU_CAP_LVL', 'SUB_MENU_A_SLUG', 'SUB_MENU_A_CB' );
  remove_submenu_page('MENU_SLUG','MENU_SLUG');

In this way click on “TopMenu” will forward to the 1st “SubMenu” and prevent “TopLevel” from being duplicate.

Alternative solution would be a renaming the “TopLevel” label inside sub-menu by adding a sub menu entity with same page_slug, menu_slug, function (callback) as it used in add_menu_page:

  add_menu_page( 'TopLevel', 'TopLevel', 'MENU_CAP_LVL', 'MENU_SLUG', 'MENU_CB' );
  add_submenu_page( 'MENU_SLUG', 'MyRenamedTopLevelMenu', 'MyRenamedTopLevelMenu', 'MENU_CAP_LVL', 'MENU_SLUG', 'MENU_CB' );
  add_submenu_page( 'MENU_SLUG', 'SubMenu', 'SubMenu', 'MENU_CAP_LVL', 'SUB_MENU_SLUG', 'SUB_MENU_CB' );

Leave a Comment