wppaste
WordPress

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:1486
Adds a submenu page.

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.
$menu_titlestring
The text to be used for the menu.
$capabilitystring
The capability required for this menu to be displayed to the user.
$menu_slugstring
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().
$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

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
31–129

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 161.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
14

14 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksdo_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.

WhenInstructionsCalls it makes
!current_user_can()31–33plugin_basename(), plugin_basename(), current_user_can()
current_user_can() && $parent_slug !== "tools.php"60–78plugin_basename(), plugin_basename(), current_user_can(), ksort(), get_plugin_page_hookname()
current_user_can() && $parent_slug === "tools.php"66–84plugin_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–82plugin_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–84plugin_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–88plugin_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–91plugin_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–90plugin_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–88plugin_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–97plugin_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–95plugin_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–94plugin_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–97plugin_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–101plugin_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–103plugin_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–101plugin_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–106plugin_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–107plugin_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–112plugin_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–110plugin_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–116plugin_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–119plugin_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–125plugin_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–123plugin_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–129plugin_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

PHPCompiledExecutedBranchesNotes
8.6-dev16131–12917
8.516131–12917
8.416131–129175 fewer instructions than PHP 8.3
8.316631–13417
8.216631–13417
8.116631–13417
7.416631–13417

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

Used by · 14

Show all 14

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

5.3.0
Added the $position parameter.from the docblock
1.5.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 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.