Creating a Menu and Submenus for plugin in WordPress
For your plugin there are two different ways to create a custom menu in WordPress. For creating the menu the first thing you’ll decide to where is locate your options page of plugin. Link of the options page link can be set in its own top-level menu like in (My Plugin Settings), it can be use as a sub menu item of an existing menu like (Settings ➪My Plugin Settings). In this article explores both methods and how to configure both.
Creating a Top-Level Menu in WordPress by Web Design Company London
In this article you will learn that how to create a new top-level menu for you plugin. If your plugin has multiple setting pages so a top-level menu is very useful for you. For creation the top-level menu you’ll use the add_menu_page function, see below:
<?php add_menu_page(page_title,menu_title,capability,menu_slug,function,icon_url,position); ?>
Now I will explain all the parameters of the function:
- page_title — This text is used for the HTML title (between <title> tags like: <title>Web Agency London</title>).
- menu_title — This is text used for the menu name in the Dashboard.
- capability — This for the Minimum user capability required to see menu.
- menu_slug — This is the Unique slug name for your menu.
- function — This is use for displays page content for the menu settings page.
- icon_url — This is use for Path to custom icon for menu: by default this path images/generic.png use.
- position — This is use for the position in the menu order the menu should appear. The menu will appear at the bottom of the menu structure by default.
Here is another good option in wordpress if you want to create submenu for your new menu. The add_submenu_page() function you can use to create additional submenu items in WordPress:
add_submenu_page(parent, page_title, menu_title,capability,menu_slug,[function]);
As we discuses the action hook in the previous articles, here first you call the admin_menu Action hook. After the basic admin panel menu structure is in place this hook is triggered. Once triggered, then call a custom function here you call your custom function cwm_create_menu() to build your menu. For creation a menu you call the add_menu_page() function in WordPress. As you seen in the example the first parameters is set your page title and the second will set menu title. The capability level to manage_options you can set also, so only an admin will see this new menu. After that you set the menu slug to cwm_main_menu, and this is the unique slug for your menu.
The custom menu now set and in this example the name is set as cwm_main_plugin_page. You have created a top-level menu, Now you are going to create the sub menu items, in the above examples there are two sub menu items created Support and the Settings. For this you will use the add_submenu_page() function. First parameter will send as the menu slug of the top-level menu you want this to fall under the main menu. You set this to cwm_main_menu, which is a unique slug for your plugin menu and you should to remember this. After that, you set the page title and menu title just like before. In the example also set the access level for viewing to manage_options. For your submenu items you also have to create a unique menu slug; this example, you’ll use a custom named value, webdesign_settings and webdesign_support. Final value is the custom function it is build the settings page for each submenu item.