Add the top-level menu and sub-menu functions in the WordPress background

Source: Internet
Author: User
This article describes how to add top-level menus and sub-menus in the WordPress background. For more information, see Add settings page-add_menu_page function
Add_menu_page (). This function is used to add top-level menus to the backend, that is, top-level menus like "appearance" and "plug-in.

Functions are described as follows:

<? Php add_menu_page ($ page_title, $ menu_title, $ capability, $ menu_slug, $ function, $ icon_url, $ position ); // page_title page title tag information // $ menu_title menu title // capability permission // menu_slug alias // function execution function // icon_url menu icon url address // position this menu item is on the menu location in, warning if the location attributes of two menu items are the same, one of them may be overwritten?>

For a function parameter, this parameter is generally a function name. if you want to call a class method in the class, you can call it in the form of array (class name, function name. If this parameter is null, the menu_slug parameter can be a file path.

In the position parameter of the above function, the default menu item location attribute is as follows:

2 Dashboard 4 Separator 5 Posts 10 Media 15 Links 20 Pages 25 Comments 59 Separator 60 Appearance 65 Plugins 70 Users 75 Tools 80 Settings 99 Separator

Well, let's explain it through examples so that Li Jufu can do it. Create a new myfuntions. php file in the default Twenty Ten topic, and then use the editor to open myfunctions. php in the functions. php file,
Add new code:

<? Php function test_function () {add_menu_page ('title title', 'menu title', 'edit _ themes', 'ashu _ slug', 'display _ function ','', 6);} function display_function () {echo 'this is the setting page';} add_action ('admin _ menu ', 'Test _ function');?>

As you can see, the menu position is 6. after comparison, we can see that it will be behind the "article" and read:

A top-level menu page is displayed...

Add sub-menu item-add_submenu_page
The function for adding a sub-menu item is as follows:

<? Php add_submenu_page ($ parent_slug, $ page_title, $ menu_title, $ capability, $ menu_slug, $ function ); // parent_slug-alias of the parent menu item // page_title -- page title information // menu_title-menu title // capability-permission // menu_slug-alias // function-executed function?>

In fact, with the first two tutorials, there is no need to talk about the usage of this function.

Instance, or use the files in the two tutorials (create a new myfuntions in the default Twenty Ten topic. php, and then in functions. php file) use the editor to open our myfunctions. php: we have added a top-level menu item in the previous tutorial. Today, we add a sub-menu item based on this top-level menu item, retain the code of yesterday, and add new code, after myfunctions. the code in php is as follows:

<? Php function test_function () {add_menu_page ('title title', 'menu title', 'edit _ themes', 'ashu _ slug', 'display _ function ','', 6);} function display_function () {echo 'this is the setting page';} add_action ('admin _ menu ', 'Test _ function '); // the following code adds a sub-menu item add_action ('admin _ menu ', 'Add _ my_custom_submenu_page'); function add_my_custom_submenu_page () {// The slug of the top menu is ashu_slug add_submenu_page ('ashu _ slug ', 'submenus', 'submenus ', 'edit _ themes ',' Ashu-submenu-page ', 'My _ submenu_page_display');} function my_submenu_page_display () {echo 'submenu item output code ';}?>

Yes:

OK. the sub-menu item is added ..

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.