Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress add submenus to custom menu

Tags:

wordpress

menu

In a wordpress plugin I'm creating, it's creating a new top-level admin menu with a sub-menu page. Here's my code:

add_menu_page('Eastview Custom', 'Eastview Custom', 5,"eastview-custom");
add_submenu_page("eastview-custom","GLS Lunch Orders","GLS Lunch",5,'glsLunch','glsLunch');

So this code creates a new admin menu, "Eastview Custom". Then it adds two sublinks: "Eastview Custom" and "GLS Lunch". The problem is that I don't want "Eastview Custom" as a sublink. I would like the only sublink to be "GLS Lunch". I can't figure out how to do this. Thanks for any help!

like image 751
Abraham Avatar asked Jul 23 '12 13:07

Abraham


People also ask

How do I add custom items to a menu in WordPress?

Add Custom Text to a Specific Menu (Easy Way) Simply go to the Appearance » Menus page and add a custom link with # sign as the URL, and the text you want to display as your Link Text. Click on the Add to Menu button to continue. WordPress will add your custom text as a menu item in the left column.

How do I add sub menu to custom post type?

add_submenu_page() this function used to add submenu in Admin Menu Page. And You will remove submenu from wordpress admin using remove_submenu_page(). add_submenu_page() by default takes 7 parameters.


2 Answers

According to the codex

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.

Which you can see on this page here: http://codex.wordpress.org/Adding_Administration_Menus#Sub-Menus

So according to the Codex you should be able to have something like the following (note that I've replaced your user levels parameter with capabilities as they are deprecated, and standardised it all to single quotes);

add_menu_page('Eastview Custom', 'Eastview Custom', 'manage_options', 'my-top-level-handle');
add_submenu_page( 'my-top-level-handle', 'GLS Lunch Orders', 'GLS Lunch', 'manage_options', 'my-top-level-handle');

Now you'd think that this would work based on the Codex - it doesn't. It won't display any sub menu items simply because there is only one of them. If you add another item you'll see that this works, ie;

add_menu_page('Eastview Custom', 'Eastview Custom', 'manage_options', 'my-top-level-handle');
add_submenu_page( 'my-top-level-handle', 'GLS Lunch Orders', 'GLS Lunch', 'manage_options', 'my-top-level-handle');
add_submenu_page( 'my-top-level-handle', 'New Item', 'New item', 'manage_options', 'new-handle');

Hope this helps a bit, shame I couldn't find the answer to the single list item!

like image 164
McNab Avatar answered Sep 19 '22 18:09

McNab


the alternative is you can remove the submenu after create main menu

add_menu_page('Eastview Custom', 'Eastview Custom', 5,"eastview-custom");
add_submenu_page("eastview-custom","GLS Lunch Orders","GLS Lunch",5,'glsLunch','glsLunch');
remove_submenu_page("eastview-custom", "eastview-custom");
like image 40
Lafif Astahdziq Avatar answered Sep 19 '22 18:09

Lafif Astahdziq