add_submenu_page( string $parent_slug, string $page_title, string $menu_title, string $capability, string $menu_slug, callable $callback = '', int|float $position = null ): string|false
- Since
- 1.5.0, 5.3.0
- Source
wp-admin/includes/plugin.php:1473
Description
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.
Compatibility
- WordPress
- since 5.3.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$parent_slugstring- The slug name for the parent menu (or the file name of a standard WordPress admin page).
$page_titlestring- The text to be displayed in the title tags of the page when the menu is selected.
$capabilitystring- The capability required for this menu to be displayed to the user.
$callbackcallableoptional- The function to be called to output the content for this page.Default:
'' $positionint|floatoptional- The position in the menu order this item should appear.Default:
null
Return value
string|false- The resulting page's hook_suffix, or false if the user does not have the capability required.
Performance profile
How much work a call to add_submenu_page() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Light
- Scaling
- Scales with input
- Instructions
- 31–129
- Plugin surface
- None
- Called by
- 14
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 161.
Nothing here hands control to plugin code.
14 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
do_action()one call below add_submenu_page()
Further down the call graph this can also reach query, option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 25 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost add_submenu_page() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!current_user_can() | 31–33 | plugin_basename(), plugin_basename(), current_user_can() |
current_user_can() && $parent_slug !== "tools.php" | 60–78 | plugin_basename(), plugin_basename(), current_user_can(), ksort(), get_plugin_page_hookname() |
current_user_can() && $parent_slug === "tools.php" | 66–84 | plugin_basename(), plugin_basename(), current_user_can(), ksort(), get_plugin_page_hookname(), get_plugin_page_hookname() |
current_user_can() && !empty($callback) && !empty($hookname) && $parent_slug !== "tools.php" | 66–82 | plugin_basename(), plugin_basename(), current_user_can(), ksort(), get_plugin_page_hookname(), add_action() |
current_user_can() && isset($submenu[$parent_slug]) && $position !== null && !$position && $position === 0 && $parent_slug !== "tools.php" | 72–84 | plugin_basename(), plugin_basename(), current_user_can(), array_unshift(), ksort(), get_plugin_page_hookname() |
current_user_can() && !empty($callback) && !empty($hookname) && $parent_slug === "tools.php" | 72–88 | plugin_basename(), plugin_basename(), current_user_can(), ksort(), get_plugin_page_hookname(), add_action(), get_plugin_page_hookname() |
current_user_can() && $position !== null && !$position && $parent_slug !== "tools.php" | 75–91 | plugin_basename(), plugin_basename(), current_user_can(), __(), sprintf(), _doing_it_wrong(), ksort(), get_plugin_page_hookname() |
current_user_can() && isset($submenu[$parent_slug]) && $position !== null && !$position && $position === 0 && $parent_slug === "tools.php" | 78–90 | plugin_basename(), plugin_basename(), current_user_can(), array_unshift(), ksort(), get_plugin_page_hookname(), get_plugin_page_hookname() |
current_user_can() && isset($submenu[$parent_slug]) && $position !== null && !$position && $position === 0 && !empty($callback) && !empty($hookname) && $parent_slug !== "tools.php" | 78–88 | plugin_basename(), plugin_basename(), current_user_can(), array_unshift(), ksort(), get_plugin_page_hookname(), add_action() |
current_user_can() && $position !== null && !$position && $parent_slug === "tools.php" | 81–97 | plugin_basename(), plugin_basename(), current_user_can(), __(), sprintf(), _doing_it_wrong(), ksort(), get_plugin_page_hookname(), get_plugin_page_hookname() |
current_user_can() && $position !== null && !$position && !empty($callback) && !empty($hookname) && $parent_slug !== "tools.php" | 81–95 | plugin_basename(), plugin_basename(), current_user_can(), __(), sprintf(), _doing_it_wrong(), ksort(), get_plugin_page_hookname(), add_action() |
current_user_can() && isset($submenu[$parent_slug]) && $position !== null && !$position && $position === 0 && !empty($callback) && !empty($hookname) && $parent_slug === "tools.php" | 84–94 | plugin_basename(), plugin_basename(), current_user_can(), array_unshift(), ksort(), get_plugin_page_hookname(), add_action(), get_plugin_page_hookname() |
13 further outcomes, up to 129 instructions
current_user_can() && isset($submenu[$parent_slug]) && $position !== null && !$position && $position === 0 && $parent_slug !== "tools.php" | 87–97 | plugin_basename(), plugin_basename(), current_user_can(), __(), sprintf(), _doing_it_wrong(), array_unshift(), ksort(), get_plugin_page_hookname() |
current_user_can() && $position !== null && !$position && !empty($callback) && !empty($hookname) && $parent_slug === "tools.php" | 87–101 | plugin_basename(), plugin_basename(), current_user_can(), __(), sprintf(), _doing_it_wrong(), ksort(), get_plugin_page_hookname(), add_action(), get_plugin_page_hookname() |
current_user_can() && isset($submenu[$parent_slug]) && $position !== null && !$position && $position === 0 && $parent_slug === "tools.php" | 93–103 | plugin_basename(), plugin_basename(), current_user_can(), __(), sprintf(), _doing_it_wrong(), array_unshift(), ksort(), get_plugin_page_hookname(), get_plugin_page_hookname() |
current_user_can() && isset($submenu[$parent_slug]) && $position !== null && !$position && $position === 0 && !empty($callback) && !empty($hookname) && $parent_slug !== "tools.php" | 93–101 | plugin_basename(), plugin_basename(), current_user_can(), __(), sprintf(), _doing_it_wrong(), array_unshift(), ksort(), get_plugin_page_hookname(), add_action() |
current_user_can() && isset($submenu[$parent_slug]) && $position !== null && !$position && $position !== 0 && $parent_slug !== "tools.php" | 94–106 | plugin_basename(), plugin_basename(), current_user_can(), absint(), array_slice(), array_slice(), array_merge(), ksort(), get_plugin_page_hookname() |
current_user_can() && isset($submenu[$parent_slug]) && $position !== null && !$position && $position === 0 && !empty($callback) && !empty($hookname) && $parent_slug === "tools.php" | 99–107 | plugin_basename(), plugin_basename(), current_user_can(), __(), sprintf(), _doing_it_wrong(), array_unshift(), ksort(), get_plugin_page_hookname(), add_action(), get_plugin_page_hookname() |
current_user_can() && isset($submenu[$parent_slug]) && $position !== null && !$position && $position !== 0 && $parent_slug === "tools.php" | 100–112 | plugin_basename(), plugin_basename(), current_user_can(), absint(), array_slice(), array_slice(), array_merge(), ksort(), get_plugin_page_hookname(), get_plugin_page_hookname() |
current_user_can() && isset($submenu[$parent_slug]) && $position !== null && !$position && $position !== 0 && !empty($callback) && !empty($hookname) && $parent_slug !== "tools.php" | 100–110 | plugin_basename(), plugin_basename(), current_user_can(), absint(), array_slice(), array_slice(), array_merge(), ksort(), get_plugin_page_hookname(), add_action() |
current_user_can() && isset($submenu[$parent_slug]) && $position !== null && !$position && $position !== 0 && !empty($callback) && !empty($hookname) && $parent_slug === "tools.php" | 106–116 | plugin_basename(), plugin_basename(), current_user_can(), absint(), array_slice(), array_slice(), array_merge(), ksort(), get_plugin_page_hookname(), add_action(), get_plugin_page_hookname() |
current_user_can() && isset($submenu[$parent_slug]) && $position !== null && !$position && $position !== 0 && $parent_slug !== "tools.php" | 109–119 | plugin_basename(), plugin_basename(), current_user_can(), __(), sprintf(), _doing_it_wrong(), absint(), array_slice(), array_slice(), array_merge(), ksort(), get_plugin_page_hookname() |
current_user_can() && isset($submenu[$parent_slug]) && $position !== null && !$position && $position !== 0 && $parent_slug === "tools.php" | 115–125 | plugin_basename(), plugin_basename(), current_user_can(), __(), sprintf(), _doing_it_wrong(), absint(), array_slice(), array_slice(), array_merge(), ksort(), get_plugin_page_hookname(), get_plugin_page_hookname() |
current_user_can() && isset($submenu[$parent_slug]) && $position !== null && !$position && $position !== 0 && !empty($callback) && !empty($hookname) && $parent_slug !== "tools.php" | 115–123 | plugin_basename(), plugin_basename(), current_user_can(), __(), sprintf(), _doing_it_wrong(), absint(), array_slice(), array_slice(), array_merge(), ksort(), get_plugin_page_hookname(), add_action() |
current_user_can() && isset($submenu[$parent_slug]) && $position !== null && !$position && $position !== 0 && !empty($callback) && !empty($hookname) && $parent_slug === "tools.php" | 121–129 | plugin_basename(), plugin_basename(), current_user_can(), __(), sprintf(), _doing_it_wrong(), absint(), array_slice(), array_slice(), array_merge(), ksort(), get_plugin_page_hookname(), add_action(), get_plugin_page_hookname() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 161 | 31–129 | 17 | |
| 8.5 | 161 | 31–129 | 17 | |
| 8.4 | 161 | 31–129 | 17 | 5 fewer instructions than PHP 8.3 |
| 8.3 | 166 | 31–134 | 17 | |
| 8.2 | 166 | 31–134 | 17 | |
| 8.1 | 166 | 31–134 | 17 | |
| 7.4 | 166 | 31–134 | 17 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 7
- plugin_basename()Gets the basename of a plugin.
- current_user_can()Returns whether the current user has the specified capability.
- _doing_it_wrong()Marks something as being incorrectly called.
- __()Retrieves the translation of $text.
- absint()Converts a value to non-negative integer.
- get_plugin_page_hookname()Gets the hook name for the administrative page of a plugin.
- add_action()Adds a callback function to an action hook.
Used by · 14
- _add_plugin_file_editor_to_tools()Adds the 'Plugin File Editor' menu item after the 'Themes File Editor' in Tools for block themes.
- _add_post_type_submenus()Adds submenus for post types.
- _add_themes_utility_last()Adds the 'Theme File Editor' menu item to the bottom of the Appearance (non-block themes) or Tools (block themes) menu.
- add_comments_page()Adds a submenu page to the Comments main menu.
- add_dashboard_page()Adds a submenu page to the Dashboard main menu.
- add_links_page()Adds a submenu page to the Links main menu.
- add_management_page()Adds a submenu page to the Tools main menu.
- add_media_page()Adds a submenu page to the Media main menu.
- add_options_page()Adds a submenu page to the Settings main menu.
- add_pages_page()Adds a submenu page to the Pages main menu.
- add_plugins_page()Adds a submenu page to the Plugins main menu.
- add_posts_page()Adds a submenu page to the Posts main menu.
Show all 14
- add_theme_page()Adds a submenu page to the Appearance main menu.
- add_users_page()Adds a submenu page to the Users/Profile main menu.
Source code
function add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $callback = '', $position = null ) { global $submenu, $menu, $_wp_real_parent_file, $_wp_submenu_nopriv, $_registered_pages, $_parent_pages; $menu_slug = plugin_basename( $menu_slug ); $parent_slug = plugin_basename( $parent_slug ); if ( isset( $_wp_real_parent_file[ $parent_slug ] ) ) { $parent_slug = $_wp_real_parent_file[ $parent_slug ]; } if ( ! current_user_can( $capability ) ) { $_wp_submenu_nopriv[ $parent_slug ][ $menu_slug ] = true; return false; } /* * If the parent doesn't already have a submenu, add a link to the parent * as the first item in the submenu. If the submenu file is the same as the * parent file someone is trying to link back to the parent manually. In * this case, don't automatically add a link back to avoid duplication. */ if ( ! isset( $submenu[ $parent_slug ] ) && $menu_slug !== $parent_slug ) { foreach ( (array) $menu as $parent_menu ) { if ( $parent_menu[2] === $parent_slug && current_user_can( $parent_menu[1] ) ) { $submenu[ $parent_slug ][] = array_slice( $parent_menu, 0, 4 ); } } } $new_sub_menu = array( $menu_title, $capability, $menu_slug, $page_title ); if ( null !== $position && ! is_numeric( $position ) ) { _doing_it_wrong( __FUNCTION__, sprintf( /* translators: %s: add_submenu_page() */ __( 'The seventh parameter passed to %s should be numeric representing menu position.' ), '<code>add_submenu_page()</code>' ), '5.3.0' ); $position = null; } if ( null === $position || ( ! isset( $submenu[ $parent_slug ] ) || $position >= count( $submenu[ $parent_slug ] ) ) ) { $submenu[ $parent_slug ][] = $new_sub_menu; } else { // Test for a negative position. $position = max( $position, 0 ); if ( 0 === $position ) { // For negative or `0` positions, prepend the submenu. array_unshift( $submenu[ $parent_slug ], $new_sub_menu ); } else { $position = absint( $position ); // Grab all of the items before the insertion point. $before_items = array_slice( $submenu[ $parent_slug ], 0, $position, true ); // Grab all of the items after the insertion point. $after_items = array_slice( $submenu[ $parent_slug ], $position, null, true ); // Add the new item. $before_items[] = $new_sub_menu; // Merge the items. $submenu[ $parent_slug ] = array_merge( $before_items, $after_items ); } } // Sort the parent array. ksort( $submenu[ $parent_slug ] ); $hookname = get_plugin_page_hookname( $menu_slug, $parent_slug ); if ( ! empty( $callback ) && ! empty( $hookname ) ) { add_action( $hookname, $callback ); } $_registered_pages[ $hookname ] = true; /*Changelog
Introduced in 1.5.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
$position parameter.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 tag, from
src/wp-admin/includes/plugin.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.