During the creation of wordpress plug-ins and themes, you often need to add menus in the background. This article will share with you how to add top-level menus in the sidebar of the wordpress background to help you better understand the concept, the function used is add_menu_page ()
Function usage:
Add_menu_page ($ page_title, $ menu_title, $ capability, $ menu_slug, $ function, $ icon_url, $ position );
The following describes the parameters:
$ Page_title: (string) (required) this parameter is the title of the sub-menu and will be displayed in the title bar of the browser. It is blank by default;
$ Menu_title: (string) (required) name of the displayed menu. The default value is null;
$ Capability: (string) (required) user permission. It defines which permissions the user will see in this submenu (see the end of the permission section). The default value is null. For more information, see capability;
$ Menu_slug: (string) (required) name of the menu displayed on the URl. The default value is null;
$ Function: name of the returned method;
$ Icon_url: (string) (optional) menu icon displayed. You can use plugin_dir_url (_ FILE _) with a 16-pixel icon width and height;
$ Position: (integer) (optional) shows the menu position. Common Location: 4, 59, or 99.
Example:
<? Php
Add_action ('admin _ menu ', 'register _ custom_menu_page ');
Function register_custom_menu_page (){
Add_menu_page ('custom menu title', 'Test menu ', 'admin', 'myin in/myplugin-index.php', '', plugins_url ('myplugin/images/icon.png '), 6 );
}
// Then put the following code into the myplugin/myplugin-index.php file
Echo "Admin Page Test ";
// Or use the following method
Add_action ('admin _ menu ', 'register _ custom_menu_page ');
Function register_custom_menu_page (){
Add_menu_page ('menu title', 'menu name', 'admin', 'customer', 'custom_menu_page ', plugins_url ('myplugin/images/icon.png'), 6 );
}
Function custom_menu_page (){
Echo "Admin Page Test ";
}
The function used to add a sub-menu to the sidebar of the wordpress background is add_submenu_page ()
Add_submenu_page ($ parent_slug, $ page_title, $ menu_title, $ capability, $ menu_slug, $ function );
The following describes the parameters:
$ Parent_slug: (string) (required) top-level menu name. You can add a sub-menu to the top-level menu or add a sub-menu to the custom top-level menu;
Situations:
1. Add a sub-menu to the dashboard: add_submenu_page ('index. Php ',... );
2. Add a sub-menu in the article: add_submenu_page ('edit. Php ',... );
3. Add a sub-menu to the media: add_submenu_page ('upload. Php ',... );
4. Add a sub-menu at the link: add_submenu_page ('Link-manager. Php ',... );
5. Add a sub-menu on the page: add_submenu_page ('edit. php? Post_type = page ',... );
6. Add a submenu to the comment: add_submenu_page ('edit-comments. Php ',... );
7. Add the submenu add_submenu_page ('edit. php? Post_type = your_post_type ',...)
8. Add a sub menu to the appearance: add_submenu_page ('themes. Php ',... );
9. Add a sub menu in the plug-in: add_submenu_page ('ins Ins. Php ',... );
10. Add a sub-menu to the User: add_submenu_page ('users. Php ',... );
11. Add a sub-menu to the tool: add_submenu_page ('tools. Php ',... );
12. Add the sub-menu add_submenu_page ('options-general. Php ',... );
$ Page_title: (string) (required) this parameter is the title of the sub-menu and will be displayed in the title bar of the browser. It is blank by default;
$ Menu_title: (string) (required) name of the displayed menu. The default value is null;
$ Capability: (string) (required) user permission. It defines which permissions the user will see in this submenu (see the end of the permission section). The default value is null. For more information, see capability;
$ Menu_slug: (string) (required) name of the menu displayed on the URl. The default value is null;
$ Function: name of all called functions. You can call this function to display the content of this sub-menu page.
Above:
Menu1
1. Add a sub-menu under "tools" in the top menu
<? Php
Add_action ('admin _ menu ', 'register _ my_custom_submenu_page ');
Function register_my_custom_submenu_page (){
Add_submenu_page ('tools. php ', 'submenus', 'submenu name', 'manage _ options', 'My-custom-submenu-page', 'My _ custom_submenu_page_callback ');
}
Function my_custom_submenu_page_callback (){
Echo '
}
?>
2. Add a sub-menu to the custom top-level menu
<? Php
Function add_diy_menu (){
Add_menu_page (_ ('top menu ') ,__ ('top menu name'), 8 ,__ FILE __, 'My _ function_menu ');
Add_submenu_page (_ FILE __, 'submenu 1', 'submenu name 1', 8, 'Your-admin-sub-menu1 ', 'My _ function_submenu1 ');
Add_submenu_page (_ FILE __, 'submenu 2', 'submenu name 2', 8, 'Your-admin-sub-menu2 ', 'My _ function_submenu2 ');
}
Function my_function_menu (){
Echo "
}
Function my_function_submenu1 (){
Echo "
}
Function my_function_submenu2 (){
Echo "
}
Add_action ('admin _ menu ', 'Add _ diy_menu ');
?>