add_submenu_page()
Add a submenu page. This function takes a capability which will be used to determine whether or not a page is included in the menu.
The function which is hooked in to handle the output of the page must check that the user has the required capability as well.
This function takes a capability which will be used to determine whether or not a page is included in the menu.
The function which is hooked in to handle the output of the page must check that the user has the required capability as well.
No Hooks.
Return
String|false
. The resulting page's hook_suffix, or false if the user does not have the capability required.
Usage
add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $callback, $position );
- $parent_slug(string) (required)
- The slug name for the parent menu (or the file name of a standard WordPress admin page).
- $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 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.
Default: '' - $position(int|float)
- The position in the menu order this item should appear.
Default: null
Examples
#1 Let's add a submenu to the "Tools" menu of the admin panel:
// Let's add a submenu to the "Tools" menu of the admin panel: add_action( 'admin_menu', 'register_my_custom_submenu_page' ); function register_my_custom_submenu_page() { add_submenu_page( 'tools.php', 'Additional Tool Page', 'Tool Name', 'manage_options', 'my-custom-submenu-page', 'my_custom_submenu_page_callback' ); } // page content function my_custom_submenu_page_callback() { ?> <div class="wrap"> <h2><?= get_admin_page_title() ?></h2> </div> <?php }
#2 Hide the page from the menu, but it will still be working
To hide a submenu item link from the main admin menu, set null
or 'options.php' to the first parameter:
<?php // Hide the page from the menu, but it will still be working add_action( 'admin_menu', 'register_my_custom_submenu_page' ); function register_my_custom_submenu_page() { add_submenu_page( 'options.php', 'Arbitrary submenu page', 'Arbitrary submenu page', 'manage_options', 'my-custom-submenu-page', 'my_custom_submenu_page_callback' ); } // page content function my_custom_submenu_page_callback() { ?> <div class="wrap"> <h2>Hidden Page Title</h2> </div> <?php }
Now going to the /wp-admin/tools.php?page=my-custom-submenu-page page, we will see the page, but it will not be visible in the admin menu:
#3 Without the function of the settings page (unofficially)
The official documentation does not have this.
In the $menu_slug parameter, you can specify the path to the options page, from the plugins directory. In this case, we do not need to specify the function which is responsible for the code of the options page. For options pages we will have a separate file.
Suppose our settings page file is in the root of the plugin folder and called options.php, then the plugin settings page is registered as follows:
add_action( 'admin_menu', 'add_options_page' ); function add_options_page(){ add_submenu_page( 'options-general.php', 'Page title', 'Menu item name', 'manage_options', basename( __DIR__ ) . '/options.php' ); }
Here basename( __DIR__ ) . '/options.php'
equals to `plugin_folder_name/options.php
'.
#4 Checking the presence of a submenu item
See add_menu_page() for examples
#5 Using a returned hook-suffix
The function returns a page hook suffix that can be used in the load-(page_hook) hook:
$hookname = add_submenu_page( 'options-general.php', $page_title, $menu_title, $capability, $menu_slug, $callback, $position ); add_action( "load-{$hookname}", 'my_admin_load' ); function my_admin_load(){ // do staff }
#6 A submenu for the custom-created menu
If you try to add a menu item to the main section that you created yourself using add_menu_page(), the first item will be a copy of the created item add_menu_page(). This can be seen throughout the entire WordPress menu.
If you need a submenu item in this scenario, you first need to create a take of the main menu and then add a submenu:
add_action( 'admin_menu', function(){ add_menu_page( 'Main additional menu', 'My main menu', 'manage_options', 'my-top-level-slug' ); add_submenu_page( 'my-top-level-slug', 'main additional menu', 'my main menu', 'manage_options', 'my-top-level-slug' ); add_submenu_page( 'my-top-level-slug', 'my-submenu', 'my-submenu settings page', 'manage_options', 'my-secondary-slug', 'page_callback_function' ); } );
Notes
- Global. Array. $submenu
- Global. Array. $menu
- Global. Array. $_wp_real_parent_file
- Global. true|false. $_wp_submenu_nopriv
- Global. Array. $_registered_pages
- Global. Array. $_parent_pages
Changelog
Since 1.5.0 | Introduced. |
Since 5.3.0 | Added the $position parameter. |