wppaste
WordPress

add_menu_page( string $page_title, string $menu_title, string $capability, string $menu_slug, callable $callback = '', string $icon_url = '', int|float $position = null ): string

Since
1.5.0
Source
wp-admin/includes/plugin.php:1378
Adds a top-level menu 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 1.5.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

$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 page 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: ''
$icon_urlstringoptional
The URL to the icon to be used for this menu.
Pass a base64-encoded SVG using a data URI, which will be colored to match the color scheme. This should begin with 'data:image/svg+xml;base64,'.
Pass the name of a Dashicons helper class to use a font icon, e.g. 'dashicons-chart-pie'.
* Pass 'none' to leave div.wp-menu-image empty so an icon can be added via CSS.Default: ''
$positionint|floatoptional
The position in the menu order this item should appear.Default: null

Return value

string
The resulting page's hook_suffix.

Performance profile

How much work a call to add_menu_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
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
54–100

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
2

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

What it touches

  • hookthird-party callbacksapply_filters()one call below add_menu_page()

Further down the call graph this can also reach option, query, 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 · 24 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost add_menu_page() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
empty($icon_url)54–64plugin_basename(), sanitize_title(), get_plugin_page_hookname()
!empty($icon_url)56–66plugin_basename(), sanitize_title(), get_plugin_page_hookname(), set_url_scheme()
!empty($callback) && !empty($hookname) && !current_user_can() && empty($icon_url)60–68plugin_basename(), sanitize_title(), get_plugin_page_hookname(), current_user_can()
!empty($callback) && !empty($hookname) && !current_user_can() && !empty($icon_url)62–70plugin_basename(), sanitize_title(), get_plugin_page_hookname(), current_user_can(), set_url_scheme()
!empty($callback) && !empty($hookname) && current_user_can() && empty($icon_url)64–72plugin_basename(), sanitize_title(), get_plugin_page_hookname(), current_user_can(), add_action()
!empty($callback) && !empty($hookname) && current_user_can() && !empty($icon_url)66–74plugin_basename(), sanitize_title(), get_plugin_page_hookname(), current_user_can(), add_action(), set_url_scheme()
empty($icon_url) && $position !== null && !$position69–77plugin_basename(), sanitize_title(), get_plugin_page_hookname(), __(), sprintf(), _doing_it_wrong()
!empty($icon_url) && $position !== null && !$position71–79plugin_basename(), sanitize_title(), get_plugin_page_hookname(), set_url_scheme(), __(), sprintf(), _doing_it_wrong()
empty($icon_url) && $position !== null && $position && isset($menu)73–77plugin_basename(), sanitize_title(), get_plugin_page_hookname(), md5(), base_convert()
!empty($icon_url) && $position !== null && $position && isset($menu)75–79plugin_basename(), sanitize_title(), get_plugin_page_hookname(), set_url_scheme(), md5(), base_convert()
!empty($callback) && !empty($hookname) && !current_user_can() && empty($icon_url) && $position !== null && !$position75–81plugin_basename(), sanitize_title(), get_plugin_page_hookname(), current_user_can(), __(), sprintf(), _doing_it_wrong()
!empty($callback) && !empty($hookname) && !current_user_can() && !empty($icon_url) && $position !== null && !$position77–83plugin_basename(), sanitize_title(), get_plugin_page_hookname(), current_user_can(), set_url_scheme(), __(), sprintf(), _doing_it_wrong()
12 further outcomes, up to 100 instructions
!empty($callback) && !empty($hookname) && !current_user_can() && empty($icon_url) && $position !== null && $position && isset($menu)79–81plugin_basename(), sanitize_title(), get_plugin_page_hookname(), current_user_can(), md5(), base_convert()
!empty($callback) && !empty($hookname) && current_user_can() && empty($icon_url) && $position !== null && !$position79–85plugin_basename(), sanitize_title(), get_plugin_page_hookname(), current_user_can(), add_action(), __(), sprintf(), _doing_it_wrong()
!empty($callback) && !empty($hookname) && !current_user_can() && !empty($icon_url) && $position !== null && $position && isset($menu)81–83plugin_basename(), sanitize_title(), get_plugin_page_hookname(), current_user_can(), set_url_scheme(), md5(), base_convert()
!empty($callback) && !empty($hookname) && current_user_can() && !empty($icon_url) && $position !== null && !$position81–87plugin_basename(), sanitize_title(), get_plugin_page_hookname(), current_user_can(), add_action(), set_url_scheme(), __(), sprintf(), _doing_it_wrong()
!empty($callback) && !empty($hookname) && current_user_can() && empty($icon_url) && $position !== null && $position && isset($menu)83–85plugin_basename(), sanitize_title(), get_plugin_page_hookname(), current_user_can(), add_action(), md5(), base_convert()
!empty($callback) && !empty($hookname) && current_user_can() && !empty($icon_url) && $position !== null && $position && isset($menu)85–87plugin_basename(), sanitize_title(), get_plugin_page_hookname(), current_user_can(), add_action(), set_url_scheme(), md5(), base_convert()
empty($icon_url) && $position !== null && isset($menu)88–90plugin_basename(), sanitize_title(), get_plugin_page_hookname(), __(), sprintf(), _doing_it_wrong(), md5(), base_convert()
!empty($icon_url) && $position !== null && isset($menu)90–92plugin_basename(), sanitize_title(), get_plugin_page_hookname(), set_url_scheme(), __(), sprintf(), _doing_it_wrong(), md5(), base_convert()
!empty($callback) && !empty($hookname) && !current_user_can() && empty($icon_url) && $position !== null && isset($menu)94plugin_basename(), sanitize_title(), get_plugin_page_hookname(), current_user_can(), __(), sprintf(), _doing_it_wrong(), md5(), base_convert()
!empty($callback) && !empty($hookname) && !current_user_can() && !empty($icon_url) && $position !== null && isset($menu)96plugin_basename(), sanitize_title(), get_plugin_page_hookname(), current_user_can(), set_url_scheme(), __(), sprintf(), _doing_it_wrong(), md5(), base_convert()
!empty($callback) && !empty($hookname) && current_user_can() && empty($icon_url) && $position !== null && isset($menu)98plugin_basename(), sanitize_title(), get_plugin_page_hookname(), current_user_can(), add_action(), __(), sprintf(), _doing_it_wrong(), md5(), base_convert()
!empty($callback) && !empty($hookname) && current_user_can() && !empty($icon_url) && $position !== null && isset($menu)100plugin_basename(), sanitize_title(), get_plugin_page_hookname(), current_user_can(), add_action(), set_url_scheme(), __(), sprintf(), _doing_it_wrong(), md5(), base_convert()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev11054–1009
8.511054–1009
8.411054–10097 fewer instructions than PHP 8.3
8.311754–1079
8.211754–1079
8.111754–1079
7.411754–1079

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 · 8

Used by · 2

Source code

function add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $callback = '', $icon_url = '', $position = null ) {	global $menu, $admin_page_hooks, $_registered_pages, $_parent_pages; 	$menu_slug = plugin_basename( $menu_slug ); 	$admin_page_hooks[ $menu_slug ] = sanitize_title( $menu_title ); 	$hookname = get_plugin_page_hookname( $menu_slug, '' ); 	if ( ! empty( $callback ) && ! empty( $hookname ) && current_user_can( $capability ) ) {		add_action( $hookname, $callback );	} 	if ( empty( $icon_url ) ) {		$icon_url   = 'dashicons-admin-generic';		$icon_class = 'menu-icon-generic ';	} else {		$icon_url   = set_url_scheme( $icon_url );		$icon_class = '';	} 	$new_menu = array( $menu_title, $capability, $menu_slug, $page_title, 'menu-top ' . $icon_class . $hookname, $hookname, $icon_url ); 	if ( null !== $position && ! is_numeric( $position ) ) {		_doing_it_wrong(			__FUNCTION__,			sprintf(				/* translators: %s: add_menu_page() */				__( 'The seventh parameter passed to %s should be numeric representing menu position.' ),				'<code>add_menu_page()</code>'			),			'6.0.0'		);		$position = null;	} 	if ( null === $position || ! is_numeric( $position ) ) {		$menu[] = $new_menu;	} elseif ( isset( $menu[ (string) $position ] ) ) {		$collision_avoider = base_convert( substr( md5( $menu_slug . $menu_title ), -4 ), 16, 10 ) * 0.00001;		$position          = (string) ( $position + $collision_avoider );		$menu[ $position ] = $new_menu;	} else {		/*		 * Cast menu position to a string.		 *		 * This allows for floats to be passed as the position. PHP will normally cast a float to an		 * integer value, this ensures the float retains its mantissa (positive fractional part).		 *		 * A string containing an integer value, eg "10", is treated as a numeric index.		 */		$position          = (string) $position;		$menu[ $position ] = $new_menu;	} 	$_registered_pages[ $hookname ] = true; 	// No parent as top level.	$_parent_pages[ $menu_slug ] = false; 	return $hookname;}

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.

About this page

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