3

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?

  • Share code of add_menu_page/add_submenu_page – Ignat B. Aug 30 '15 at 12:13
6

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' );
-2

You can easily handle via css.

Like

.class_name{
display:none;
}

Hopefully it will work for you.

  • 1
    I managed to do it in CSS but it's dirty and not a great solution. .toplevel_page_PLUGIN_NAME .wp-submenu .wp-first-item { display: none; } – hitautodestruct Aug 30 '15 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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