Friday , 17 November 2017
Home / Programming / WordPress / Plugins / WordPress Admin Submenus – WordPress Plugin Tutorial
WordPress Admin Submenus
WordPress Admin Submenus

WordPress Admin Submenus – WordPress Plugin Tutorial

This post is part of the WordPress Plugin tutorial series. To find a link to all tutorials in order, then please click here.

In our last lesson, we discussed how to create top-level menus. We’ll now learn how to create WordPress admin submenus. We’ll be working with the same plugin we created in our previous lesson.

Let’s update our menus.inc.php file to this.

WordPress Admin Submenus

As you can see we used the add_submenu_page() function to add a submenu. It takes in 6 parameters which are the following.

  • Parent Slug – This defines which top-level menu our WordPress admin submenus should appear in. In our case, we gave our top-level menu the slug of __FILE__ which would correspond to the 4th parameter in the add_menu_page() function. 
  • Page Title – This is the page title you see at the top of your browser.
  • Menu Title – This is the name for your submenu.
  • Capability – This is the same definition I gave for when I was explaining top-level menus. This is what the user must be permitted to do in order to see this submenu.
  • Menu Slug – This is the URL for our submenu. In our case we once again use the magic constant __FILE__ plus we append _submenu because our top-level menu is already using the magic constant __FILE__.
  • Function – This parameter is the only optional parameter. This is the function we call when someone clicks on this menu item.

As you can see, we aren’t allowed to define an icon or a position. WordPress doesn’t use icons for submenus and submenus are ordered according to the order they’re defined.

Before we go any further, we must update our display-options.inc.php file.

We defined the function we called in our add_submenu_page() function. Now we can refresh the dashboard and if you hover your mouse over our newly created top-level menu, we can see the submenu we created. You can also click it and you should see the message “This is our submenu options page”.

WordPress Admin Submenus
WordPress Admin Submenus

You will also notice WordPress automatically makes your top-level menu item into a submenu as well.

Creating A Submenu for WordPress defined Menus

Sometimes you don’t need your own top-level menu. You may want to just have a submenu because your plugin is so simple and has few settings. I highly suggest you put your submenu in an already defined top-level menu because only plugins that absolutely have a variety of options should have a top-level menu. If you really need it, then create a top-level menu, otherwise use a WordPress defined top-level menu.

Change your add_submenu_page in your menus.inc.php file to this.

We simply changed the first parameter to options-general.php. This is the name for the settings top-level menu. WordPress has a list of Menu slugs for the admin menus here. Take a look at the menu slugs you can use in order to add your submenu to them. This is how you create WordPress admin submenus.

About Jasko Koyn

Check Also

WordPress Meta Boxes

WordPress Meta Boxes – WordPress Plugin Tutorial

WordPress meta boxes are used to display certain information or options in a page. It’s …

4 comments

  1. This answer is very helpful. But i have a query in this solution. I already did same in my site code. But the same paret menu link also showing into the child menu. As per your example:

    You create a top main menu named “Top level menu” and create a sub menu for this name “Submenu”. But it showing the same parent top menu name ‘Top level menu’ into the sub menu,whether you just create only one sub menu.

    So my question how can i show only submenu under top main menu not top menu itself ?.

    Please give answer ASAP.

    Advance Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *

ULTIMATE WORDPRESS DEVELOPMENT COURSE
Want to learn how to create your own custom WordPress plugins and themes? Check out my course!
Get 93% off my course. You'll also learn how to integrate WooCommerce and BuddyPress into your projects. This is a limited time offer
Yes, I would like to get 93% off your course!
No Thanks!
close-link