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 the add_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' );
In the above code, we use the add_menu_page()
function to create a menu page named "My Plugin". my_plugin_menu_callback
in 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 the add_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' );
In the above sample code, we use the add_submenu_page()
function to create a submenu item named "Submenu Page". my_plugin_submenu_callback
in 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 '<h1 id="My-Plugin-Menu">My Plugin Menu</h1>'; echo '<p>Welcome to My Plugin Menu. You can add your content here.</p>'; } function my_plugin_submenu_callback() { echo '<h1 id="Submenu-Page">Submenu Page</h1>'; echo '<p>Welcome to Submenu Page. You can add your content here.</p>'; }
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 the add_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 '<h1 id="Submenu-Page">Submenu Page 1</h1>'; echo '<p>Welcome to Submenu Page 1. You can add your content here.</p>'; } function my_plugin_submenu_callback2() { echo '<h1 id="Submenu-Page">Submenu Page 2</h1>'; echo '<p>Welcome to Submenu Page 2. You can add your content here.</p>'; }
In the above code, we use the add_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 the add_menu_page()
function; then, add submenu items through the add_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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



There will be many AI creation functions in the Doubao app, so what functions does the Doubao app have? Users can use this software to create paintings, chat with AI, generate articles for users, help everyone search for songs, etc. This function introduction of the Doubao app can tell you the specific operation method. The specific content is below, so take a look! What functions does the Doubao app have? Answer: You can draw, chat, write articles, and find songs. Function introduction: 1. Question query: You can use AI to find answers to questions faster, and you can ask any kind of questions. 2. Picture generation: AI can be used to create different pictures for everyone. You only need to tell everyone the general requirements. 3. AI chat: can create an AI that can chat for users,

Both vivox100s and x100 mobile phones are representative models in vivo's mobile phone product line. They respectively represent vivo's high-end technology level in different time periods. Therefore, the two mobile phones have certain differences in design, performance and functions. This article will conduct a detailed comparison between these two mobile phones in terms of performance comparison and function analysis to help consumers better choose the mobile phone that suits them. First, let’s look at the performance comparison between vivox100s and x100. vivox100s is equipped with the latest

With the rapid development of the Internet, the concept of self-media has become deeply rooted in people's hearts. So, what exactly is self-media? What are its main features and functions? Next, we will explore these issues one by one. 1. What exactly is self-media? We-media, as the name suggests, means you are the media. It refers to an information carrier through which individuals or teams can independently create, edit, publish and disseminate content through the Internet platform. Different from traditional media, such as newspapers, television, radio, etc., self-media is more interactive and personalized, allowing everyone to become a producer and disseminator of information. 2. What are the main features and functions of self-media? 1. Low threshold: The rise of self-media has lowered the threshold for entering the media industry. Cumbersome equipment and professional teams are no longer needed.

As Xiaohongshu becomes popular among young people, more and more people are beginning to use this platform to share various aspects of their experiences and life insights. How to effectively manage multiple Xiaohongshu accounts has become a key issue. In this article, we will discuss some of the features of Xiaohongshu account management software and explore how to better manage your Xiaohongshu account. As social media grows, many people find themselves needing to manage multiple social accounts. This is also a challenge for Xiaohongshu users. Some Xiaohongshu account management software can help users manage multiple accounts more easily, including automatic content publishing, scheduled publishing, data analysis and other functions. Through these tools, users can manage their accounts more efficiently and increase their account exposure and attention. In addition, Xiaohongshu account management software has

PHP Tips: Quickly implement the function of returning to the previous page. In web development, we often encounter the need to implement the function of returning to the previous page. Such operations can improve the user experience and make it easier for users to navigate between web pages. In PHP, we can achieve this function through some simple code. This article will introduce how to quickly implement the function of returning to the previous page and provide specific PHP code examples. In PHP, we can use $_SERVER['HTTP_REFERER'] to get the URL of the previous page

"Exploring Discuz: Definition, Functions and Code Examples" With the rapid development of the Internet, community forums have become an important platform for people to obtain information and exchange opinions. Among the many community forum systems, Discuz, as a well-known open source forum software in China, is favored by the majority of website developers and administrators. So, what is Discuz? What functions does it have, and how can it help our website? This article will introduce Discuz in detail and attach specific code examples to help readers learn more about it.

Detailed explanation of the functions and functions of GDM under Linux In the Linux operating system, GDM (GNOMEDisplayManager) is a graphical login manager that provides an interface for users to log in and log out of the system. GDM is usually part of the GNOME desktop environment, but can be used by other desktop environments as well. The role of GDM is not only to provide a login interface, but also includes user session management, screen saver, automatic login and other functions. The functions of GDM mainly include the following aspects:

PHP is a server-side scripting language widely used in web development. Its main function is to generate dynamic web content. When combined with HTML, it can create rich and colorful web pages. PHP is powerful. It can perform various database operations, file operations, form processing and other tasks, providing powerful interactivity and functionality for websites. In the following articles, we will further explore the role and functions of PHP, with detailed code examples. First, let’s take a look at a common use of PHP: dynamic web page generation: P
