I want to check whether a certain top-level menu is already present or not in the WordPress admin interface:
- If it is present then I want to create a submenu in it.
- Otherwise I want to create the top-level menu and then a submenu.
I have a few small plugins that I want organized in a single top-level menu, and then a submenu for each plugin. But how can I check for the existence of the top-level menu?
You can do this using the global variable
$menu
this will return an array or items, these items has a particular index in which the name of the menu is being stored you can loop through the array to find the needed index and if is found then you just add the submenu page otherwise you can create it.Another option is, to use the function
menu_page_url()
:Usage
The function takes two arguments:
$echo
, which defaults to true.The second parameter is the important part: Set it to
false
to get a return value. If you don’t do this, then the function will echo the URL instead of returning it.Sample code: