Home> CMS Tutorial> WordPress> body text

How to add multi-level menu functionality to WordPress plugin

王林
Release: 2023-09-05 13:06:15
Original
688 people have browsed it

How to add multi-level menu functionality to WordPress plugin

How to add multi-level menu functionality to WordPress plug-ins

In WordPress, plug-ins are an important part of extending website functionality. By default, WordPress provides some menu features such as top navigation menu and sidebar menu. However, sometimes we need to add custom multi-level menu functions to our plug-ins to facilitate users to access various functions of the plug-in.

This article will teach you how to add multi-level menu functionality to a WordPress plugin and provide some code examples. I hope it will be helpful to your plug-in development work.

Step 1: Create a plug-in menu page

First, we need to create a page to be used as a plug-in menu. In WordPress, this can be achieved by calling theadd_menu_page()function.

function my_plugin_menu_page() { add_menu_page( 'My Plugin', // 页面标题 'My Plugin', // 菜单标题 'manage_options', // 用户权限 'my-plugin', // 菜单 slug 'my_plugin_menu_callback', // 页面回调函数 'dashicons-admin-plugins', // 菜单图标 99 // 菜单位置 ); } add_action( 'admin_menu', 'my_plugin_menu_page' );
Copy after login

In the above code, we use theadd_menu_page()function to create a menu page named "My Plugin".my_plugin_menu_callbackin the parameters is our custom page callback function, which is used to render the content of the menu page.

Step 2: Add submenu items

If we want to add a multi-level menu function, we can use theadd_submenu_page()function to achieve it. Here is a sample code:

function my_plugin_submenu_page() { add_submenu_page( 'my-plugin', // 父菜单 slug 'Submenu Page', // 子菜单标题 'Submenu Page', // 页面标题 'manage_options', // 用户权限 'my-plugin-submenu', // 子菜单 slug 'my_plugin_submenu_callback' // 页面回调函数 ); } add_action( 'admin_menu', 'my_plugin_submenu_page' );
Copy after login

In the above sample code, we use theadd_submenu_page()function to create a submenu item named "Submenu Page".my_plugin_submenu_callbackin the parameters is our customized submenu page callback function.

Step 3: Customize the content of the menu page

Now, we need to add customized content to the menu page. Here is a sample code:

function my_plugin_menu_callback() { echo '

My Plugin Menu

'; echo '

Welcome to My Plugin Menu. You can add your content here.

'; } function my_plugin_submenu_callback() { echo '

Submenu Page

'; echo '

Welcome to Submenu Page. You can add your content here.

'; }
Copy after login

In the above sample code, we have added some simple HTML content in the page callback function. You can customize the content of the menu page according to your needs.

Step 4: Add more submenu items

If we need to add multiple submenu items, we can call theadd_submenu_page()function repeatedly. The following is a sample code:

function my_plugin_submenu_page() { add_submenu_page( 'my-plugin', // 父菜单 slug 'Submenu Page 1', // 子菜单标题 'Submenu Page 1', // 页面标题 'manage_options', // 用户权限 'my-plugin-submenu1', // 子菜单 slug 'my_plugin_submenu_callback1' // 页面回调函数 ); add_submenu_page( 'my-plugin', // 父菜单 slug 'Submenu Page 2', // 子菜单标题 'Submenu Page 2', // 页面标题 'manage_options', // 用户权限 'my-plugin-submenu2', // 子菜单 slug 'my_plugin_submenu_callback2' // 页面回调函数 ); } // 页面回调函数 function my_plugin_submenu_callback1() { echo '

Submenu Page 1

'; echo '

Welcome to Submenu Page 1. You can add your content here.

'; } function my_plugin_submenu_callback2() { echo '

Submenu Page 2

'; echo '

Welcome to Submenu Page 2. You can add your content here.

'; }
Copy after login

In the above code, we use theadd_submenu_page()function to create two submenu items respectively, and specify different submenu slugs and pages respectively. Callback.

Summary

Through the above steps, we can add multi-level menu functionality to the WordPress plugin. First, create the plug-in menu page through theadd_menu_page()function; then, add submenu items through theadd_submenu_page()function; finally, add content to the menu page through the custom page callback function .

I hope this article can help you implement multi-level menu functionality in WordPress plug-in development. Remember to follow best practices and security principles when developing plugins.

The above is the detailed content of How to add multi-level menu functionality to WordPress plugin. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!