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

I have written a custom plugin for wordpress.

Once I add a sub-menu to the plugin I get a double entry for the top level item:

enter image description here

I would like to disable the second “TopLevel” menu item so it would appear like so:

enter image description here

So, How do I disable the Top leve sub menu?

Solutions Collecting From Web of "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' );

You can easily handle via css.

Like

.class_name{
display:none;
}

Hopefully it will work for you.