How to add menus and submenus in WordPress admin panel

Sometimes you need to provide pages for your WordPress plugin or theme to show some configurable features. So it is obvious that you have to provide a link in your admin panel for these pages. Providing menus in admin sidebar is a good idea and you can also create submenus if you want to specify the features.

1. Creating menu and a page –

you just need to add this code in functions.php file of your plugin or theme.

 

second parameter in the above code is name of callback function. In callback function you have to write what you want to alter in admin menu.

 

As you can see in personalised_menu() function I just used add_menu_page(). This function allow you to create a menu in admin sidebar and map that menu to a page.

In personlised_menu() function

  • First parameter is page title.
  • Second parameter is menu title.
  • Third one is capability, which decide that users with which roles can see this menu and page. You can find a list of all WordPress capabilities here – https://codex.wordpress.org/Roles_and_Capabilities
  • Fourth parameter is menu slug, which is used for creating page URL. Keep this unique.
  • Fifth parameter is page callback function. This is the name of function which returns the output you want to render in page.
  • Sixth parameter is for icon, either you can provide a URL of image or you can choose predefined WordPress icons. https://developer.wordpress.org/resource/dashicons/

2. Creating a menu, 2 submenus and 3 pages.

If you want to create a menu which contains 2 submenus and 3 pages respectively.

you need to add this code in functions.php file of your plugin or theme.

Then you have to make changes in personalised_menu() function like.

 

Please note that add_submenu_page() function has almost same parameters as add_menu_page() but it has first parameter extra as slug name of parent menu and it does not have parameter for icon.

If you want to make a single page for menu and for first submenu then you need to provide same callback function in add_menu_page() and in first add_submenu_page() function and slug name also should be same as parent’s menu slug name.

 

Please follow and like us:
20