WordPress plugin creation tutorial (3): how to add a menu

Source: Internet
Author: User

In the previous article, we wrote a Simple plug-in to let everyone know about the simple production of the plug-in. This article is a further step. When we activate the plug-in, the menu will be displayed in the background, click the menu to display the defined information. After being activated, a menu is displayed in the WordPress background. There are multiple sub-menus, such:

1. Add a same-level main menu in the WordPress background and add a sub-menu under the main menu

// Add_menu_page ($ page_title, $ menu_title, $ capability, $ menu_slug, $ function, $ icon_url, $ position);// Official document: http://codex.wordpress.org/Function_Reference/add_menu_page// about parameters: // $ page_title :( string) (required) this parameter is the title of the submenu and will be displayed in the browser's title bar, empty by default; // $ menu_title: (string) (required) Name of the displayed menu, empty by default; // $ capability: (string) (required) user permission, defines which permissions the user will see this sub-menu (permissions part at the end of the article), the default is empty; about user permissions official documentation: http://codex.wordpress.org/Roles_and_Capabilities// $ 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) the displayed menu icon. Use plugin_dir_url (_ file _), and the icon width and height are 16 pixels; // $ position :( integer) (optional) to display the menu position. Common location: 4, 59, or 99.// Add_submenu_page ($ parent_slug, $ page_title, $ menu_title, $ capability, $ menu_slug, $ function );// Official document: http://codex.wordpress.org/Function_Reference/add_submenu_page// about parameters: // $ parent_slug :( string) (required) top menu name, you can add our sub menu in top menu, you can also add sub-menus to the custom top-level menu. (that is, the $ menu_slug parameter in the add_menu_page () function) // $ page_title :( string) (required) this parameter is the title of the sub-menu and will be displayed in the title bar of the browser. The default value is null. // $ menu_title: (string) (required) the name of the displayed menu. The default value is null; // $ capability: (string) (required) user permission. It defines which permissions the user has will see this sub-menu (see the end of the article for permissions). The default value is null; official document about user permissions: http://codex.wordpress.org/Roles_and _ Capabilities // $ menu_slug :( string) (required) the name of the menu displayed on the URL. The default value is null. // $ function: name of all called functions, call this function to display the content of this Sub-menu page. // Display the main menu and submenu add_action ('admin _ menu ', 'add _ settings_menu'); function add_settings_menu () {add_menu_page (_ ('custom menu title '), _ ('test menu '), 'administrator', _ file __, 'my _ function_menu ', false, 100); add_submenu_page (_ file __, 'submenu 1', 'test submenu 1', 'admin', 'your-admin-sub-menu1 ', 'My _ function_submenu1'); add_submenu_page (_ file __, 'sub-menu 2', 'test sub-menu 2', 'admin', 'your-admin-sub-menu2 ', 'My _ function_submenu2'); add_submenu_page (_ file __, 'sub menu 3', 'test sub menu 3', 'admin', 'your-admin-sub-menu3 ', 'My _ function_submenu3');} function my_function_menu () {echo "<H2> test menu settings </H2>";} function my_function_submenu1 () {echo "<H2> test sub-menu settings 1 </H2> ";} function my_function_submenu2 () {echo "<H2> test sub-menu setting 2 </H2>";} function my_function_submenu3 () {echo "<H2> test sub-menu settings 3 </H2> ";}

 

2. Add a sub-menu in the WordPress background appearance menu

// Add_theme_page ($ page_title, $ menu_title, $ capability, $ menu_slug, $ Function); // Official document: http://codex.wordpress.org/Function_Reference/add_theme_page// about parameters: // $ page_title: (string) (required) displays the menu title, which is displayed in the browser's title bar, empty by default; // $ menu_title: (string) (required) indicates the name of the menu displayed. The default value is null. // $ capability: (string) (required) indicates the user permission, defines which permissions the user will see this sub-menu (permissions part at the end of the article), is empty by default; about user permissions official documentation: http://codex.wordpress.org/Roles_and_Capabilities// $ menu_slug: (string) (required) the name of the menu displayed on the URL. null by default; // $ function: (C Allback) (optional) call this function to display the content of this subpage. Default: ''// Add a sub-menu add_action ('admin _ menu ', 'appearance _ submenu') in the WordPress background appearance menu; function appearance_submenu () {add_theme_page (_ ('plug-In settings'), _ ('plug-In settings'), 'admin', 'your-unique-identifier ', 'Add _ appearance_submenu ');} function add_appearance_submenu () {echo' <div> <p> This is where the theme plug-in is set. </P> </div> ';}

 

3. Add a sub-menu in the WordPress background dashboard.

// Add_dashboard_page ($ page_title, $ menu_title, $ capability, $ menu_slug, $ function );// Official document: http://codex.wordpress.org/Function_Reference/add_dashboard_page// about parameters: // $ page_title: (string) (required) display menu title, will be displayed in the browser title bar, default is empty; // $ menu_title: (string) (required) indicates the name of the menu displayed. The default value is null. // $ capability: (string) (required) indicates the user permission, defines which permissions the user will see this sub-menu (permissions part at the end of the article), is empty by default; about user permissions official documentation: http://codex.wordpress.org/Roles_and_Capabilities// $ menu_slug: (string) (required) the name of the menu displayed on the URL. the default value is null. // $ function: (Callback) (optional) call this function to display the content of this subpage. Default: ''// Add a sub-menu add_action ('admin _ menu ', 'dashboard _ submenu') in the WordPress background dashboard; function dashboard_submenu () {add_dashboard_page (_ ('dashboard settings'), _ ('dashboard settings'), 'read', 'your-unique-identifier ', 'Add _ dashboard_submenu ');} function add_dashboard_submenu () {echo' <div> <p> This is where the dashboard menu is set. </P> </div> ';}

 

4. Add a sub-menu in the WordPress background comment.

// Add_comments_page ($ page_title, $ menu_title, $ capability, $ menu_slug, $ function );// Official document: http://codex.wordpress.org/Function_Reference/add_comments_page// about parameters: // $ page_title: (string) (required) display menu title, will be displayed in the browser title bar, default is empty; // $ menu_title: (string) (required) indicates the name of the menu displayed. The default value is null. // $ capability: (string) (required) indicates the user permission, defines which permissions the user will see this sub-menu (permissions part at the end of the article), is empty by default; about user permissions official documentation: http://codex.wordpress.org/Roles_and_Capabilities// $ menu_slug: (string) (required) the name of the menu displayed on the URL. the default value is null. // $ function: (Callback) (optional) call this function to display the content of this subpage. Default: ''// Add a sub-menu add_action ('admin _ menu ', 'comments _ submenu') in the WordPress background comment; function comments_submenu () {add_comments_page (_ ('comments menu '), _ ('My comments'), 'read', 'your-unique-Identifier-Comments ', 'Add _ comments_submenu ');} function add_comments_submenu () {echo' <div> <p> This is where the comment menu is set. </P> </div> ';}

 

5. The above describes different display methods and positions. In addition

Add_posts_page (); // Add the sub-menu add_media_page () in the article; // Add the sub-menu add_links_page () to the media; // Add the sub-menu add_pages_page () at the link (); // Add the sub-menu add_plugins_page () on the page; // Add the sub-menu add_users_page () in the plug-in; // Add the sub-menu add_management_page () to the user (); // Add the sub-menu add_options_page () in the tool; // Add sub-menus in the settings. // These are the methods for adding menus and sub-menus in the WordPress background. You can choose based on your needs.

 

Attached: permissions of each user role. For more information, see the corresponding table capability. role table. The horizontal coordinates are user roles, and the vertical coordinates are user permissions. The highlighted Blue section in the middle is the permissions of each role.

WordPress plugin creation tutorial (3): how to add a menu

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.