The add_plugins_page() WordPress PHP function adds a submenu page to the Plugins main menu. It requires certain capabilities to determine whether the page is included in the menu, and it also checks if the user has the required capability.
Usage
Here’s a generic usage of add_plugins_page():
add_plugins_page('My Plugin Page', 'My Plugin', 'manage_options', 'my-plugin', 'my_plugin_page_callback');
In this example, ‘My Plugin Page’ is the title of the page, ‘My Plugin’ is the menu title, ‘manage_options’ is the capability, ‘my-plugin’ is the unique slug for the menu, and ‘my_plugin_page_callback’ is the callback function that outputs the content for the page.
Parameters
- $page_title (string – Required): The text to be displayed in the title tags of the page when the menu is selected.
- $menu_title (string – Required): The text to be used for the menu.
- $capability (string – Required): The capability required for this menu to be displayed to the user.
- $menu_slug (string – Required): The slug name to refer to this menu by (should be unique for this menu).
- $callback (callable – Optional): The function to be called to output the content for this page. Default is empty string.
- $position (int – Optional): The position in the menu order this item should appear. Default is null.
More information
See WordPress Developer Resources: add_plugins_page()
Examples
Basic Plugin Page
In this example, we’ll create a basic plugin page.
function my_plugin_menu() { add_plugins_page('My Plugin Page', 'My Plugin', 'manage_options', 'my-plugin', 'my_plugin_page_callback'); } add_action('admin_menu', 'my_plugin_menu');
This will add a new submenu page to the Plugins main menu.
Plugin Page with a Custom Position
You can define a custom position for your plugin page. Let’s make it the second item in the Plugins menu.
function my_plugin_menu() { add_plugins_page('My Plugin Page', 'My Plugin', 'manage_options', 'my-plugin', 'my_plugin_page_callback', 2); } add_action('admin_menu', 'my_plugin_menu');
Now, ‘My Plugin’ will appear as the second item in the Plugins menu.
Plugin Page with Custom Capability
If you want to restrict access to your plugin page, you can define a custom capability.
function my_plugin_menu() { add_plugins_page('My Plugin Page', 'My Plugin', 'activate_plugins', 'my-plugin', 'my_plugin_page_callback'); } add_action('admin_menu', 'my_plugin_menu');
Now, only users with the ‘activate_plugins’ capability can view ‘My Plugin’ in the Plugins menu.
Plugin Page with Callback Function
You can define a callback function to display custom content on your plugin page.
function my_plugin_menu() { add_plugins_page('My Plugin Page', 'My Plugin', 'manage_options', 'my-plugin', 'my_plugin_page_callback'); } function my_plugin_page_callback() { echo 'Welcome to My Plugin Page!'; } add_action('admin_menu', 'my_plugin_menu');
Now, when you click on ‘My Plugin’ in the Plugins menu, you’ll see ‘Welcome to My Plugin Page!’.