Add menu and submenu in WordPress admin panel

When we add a new plugin we see a new menu item added on the WordPress admin panel. In this tutorial we will learn how easily we can add admin menu and submenus on WordPress.

Here is the thing we will make.

Members is the main menu and on mouse over it will show Members with a submenu Add Members.

WordPress have 2 cool functions for this.

add_menu_page()

add_submenu_page()

add_menu_page() create main menu like Members and add_submenu_page() create submenu like Add Members.

Here is the code.


function mysite_admin_menu(){
add_menu_page('Members', 'Members', 'manage_options', 'members-slug', 'members_function');
add_submenu_page( 'members-slug', 'Add Members', 'Add Members', 'manage_options', 'add-members-slug', 'add_members_function');
}

add_action('admin_menu', 'mysite_admin_menu');

Here is some details about the parameters

‘Members’ = Page Title

‘Members’ = Menu Title

‘manage-options’ = Capabilities required for this menu

‘members-slug’ = Menu slug shows on the url

‘members_function’ = Output of this function will show on the menu page.

In add_submenu_page() function we see everything is same except ‘members-slug’ on the first parameter. This slug refers the dropdown menu under the main menu.

Well if you want to show something when click the menu or submenu then use the function that used on functions parameter. Here is an example.


function add_members_function(){
 echo "Add New Members";
}

 

So when you  click on Add Members menu, you will see the output Add New Members  on top of the page

Hope this tutorials helps.

 

2 thoughts on “Add menu and submenu in WordPress admin panel”

Leave a Reply

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