Wordpress add submenus to custom menu
Solution 1
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!
Solution 2
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");
Abraham
Updated on June 08, 2022Comments
-
Abraham almost 2 years
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!
-
Abraham almost 12 yearsOK, thank you. Sorry I didn't reply earlier. That's fine - on the website I'm using this on, there will be more than one submenu page. I just wanted to know how to remove the duplicate link. Thanks!
-
Francisco Corrales Morales over 10 yearsGreat that worked for me !, I had a hard time looking in the Wordpress documentation. Thank you.
-
McNab over 10 yearsNo worries! Glad to hear it's still helping ;)
-
HCN almost 9 yearsWorked for me.. Thanks a lot!
-
iSaumya over 7 yearsMind blowing answer. Exactly what I was looking for. Thank you very much.
-
McNab over 7 yearsNice one @iSaumya, glad it helped.
-
huykon225 over 6 yearsHow can I add third level of wordpress dashboard =))
-
McNab almost 6 yearsI just found my old answer to this question whilst googling, as I had the same problem again (always a bit of a surprise to find your own answer :)). Your answer should be the accepted answer to this question as it actually solves the problem. Nice one!
-
Lafif Astahdziq almost 6 years@McNab haha, sounds funny. Glad if it help