The add_submenu_page() WordPress PHP function adds a submenu to an existing menu item in the WordPress admin interface. It requires a set of parameters to determine the parent menu, text to be displayed, user capability to view the menu, and the slug name for the menu.
Usage
Here’s a basic example that adds a “Custom Submenu” under the “Tools” parent menu:
add_submenu_page(
'tools.php', // Parent slug
'Custom Submenu Title', // Page title
'Custom Submenu', // Menu title
'manage_options', // Capability
'custom-submenu-slug', // Menu slug
'display_custom_submenu_content' // Callback function
);
In this example, ‘display_custom_submenu_content’ is a function that you would define elsewhere in your code to display the content of your new submenu page.
Parameters
- $parent_slug (string) – The slug name for the parent menu, or the file name of a standard WordPress admin page.
- $page_title (string) – The text to be displayed in the title tags of the page when the menu is selected.
- $menu_title (string) – The text to be used for the menu.
- $capability (string) – The capability required for this menu to be displayed to the user.
- $menu_slug (string) – The slug name to refer to this menu by. Should be unique for this menu and only include lowercase alphanumeric, dashes, and underscores characters to be compatible with sanitize_key().
- $callback (callable) – The function to be called to output the content for this page. Optional.
- $position (int|float) – The position in the menu order this item should appear. Optional. Default: null.
More Information
See WordPress Developer Resources: add_submenu_page()
Examples
Adding a Submenu Page under Posts
This example adds a submenu page under the “Posts” menu.
function my_custom_submenu_page() {
add_submenu_page(
'edit.php', // Parent slug
'My Submenu Page', // Page title
'My Submenu', // Menu title
'manage_options', // Capability
'my-submenu', // Menu slug
'display_my_submenu_page' // Callback function
);
}
add_action('admin_menu', 'my_custom_submenu_page');
function display_my_submenu_page() {
echo 'Hello, this is my submenu page!';
}
Adding a Submenu Page under Settings
This example adds a submenu page under the “Settings” menu.
function my_settings_submenu_page() {
add_submenu_page(
'options-general.php',
'My Settings Submenu Page',
'My Settings Submenu',
'manage_options',
'my-settings-submenu',
'display_my_settings_submenu_page'
);
}
add_action('admin_menu', 'my_settings_submenu_page');
function display_my_settings_submenu_page() {
echo 'Welcome to my settings submenu page!';
}