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
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.
$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:
'' $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
- Scaling
- Constant
- Instructions
- 54–100
- Plugin surface
- None
- Called by
- 2
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 110.
Nothing here hands control to plugin code.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
empty($icon_url) | 54–64 | plugin_basename(), sanitize_title(), get_plugin_page_hookname() |
!empty($icon_url) | 56–66 | plugin_basename(), sanitize_title(), get_plugin_page_hookname(), set_url_scheme() |
!empty($callback) && !empty($hookname) && !current_user_can() && empty($icon_url) | 60–68 | plugin_basename(), sanitize_title(), get_plugin_page_hookname(), current_user_can() |
!empty($callback) && !empty($hookname) && !current_user_can() && !empty($icon_url) | 62–70 | plugin_basename(), sanitize_title(), get_plugin_page_hookname(), current_user_can(), set_url_scheme() |
!empty($callback) && !empty($hookname) && current_user_can() && empty($icon_url) | 64–72 | plugin_basename(), sanitize_title(), get_plugin_page_hookname(), current_user_can(), add_action() |
!empty($callback) && !empty($hookname) && current_user_can() && !empty($icon_url) | 66–74 | plugin_basename(), sanitize_title(), get_plugin_page_hookname(), current_user_can(), add_action(), set_url_scheme() |
empty($icon_url) && $position !== null && !$position | 69–77 | plugin_basename(), sanitize_title(), get_plugin_page_hookname(), __(), sprintf(), _doing_it_wrong() |
!empty($icon_url) && $position !== null && !$position | 71–79 | plugin_basename(), sanitize_title(), get_plugin_page_hookname(), set_url_scheme(), __(), sprintf(), _doing_it_wrong() |
empty($icon_url) && $position !== null && $position && isset($menu) | 73–77 | plugin_basename(), sanitize_title(), get_plugin_page_hookname(), md5(), base_convert() |
!empty($icon_url) && $position !== null && $position && isset($menu) | 75–79 | plugin_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 && !$position | 75–81 | plugin_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 && !$position | 77–83 | plugin_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–81 | plugin_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 && !$position | 79–85 | plugin_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–83 | plugin_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 && !$position | 81–87 | plugin_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–85 | plugin_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–87 | plugin_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–90 | plugin_basename(), sanitize_title(), get_plugin_page_hookname(), __(), sprintf(), _doing_it_wrong(), md5(), base_convert() |
!empty($icon_url) && $position !== null && isset($menu) | 90–92 | plugin_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) | 94 | plugin_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) | 96 | plugin_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) | 98 | plugin_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) | 100 | plugin_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 110 | 54–100 | 9 | |
| 8.5 | 110 | 54–100 | 9 | |
| 8.4 | 110 | 54–100 | 9 | 7 fewer instructions than PHP 8.3 |
| 8.3 | 117 | 54–107 | 9 | |
| 8.2 | 117 | 54–107 | 9 | |
| 8.1 | 117 | 54–107 | 9 | |
| 7.4 | 117 | 54–107 | 9 |
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
- plugin_basename()Gets the basename of a plugin.
- sanitize_title()Sanitizes a string into a slug, which can be used in URLs or HTML attributes.
- get_plugin_page_hookname()Gets the hook name for the administrative page of a plugin.
- current_user_can()Returns whether the current user has the specified capability.
- add_action()Adds a callback function to an action hook.
- set_url_scheme()Sets the scheme for a URL.
- _doing_it_wrong()Marks something as being incorrectly called.
- __()Retrieves the translation of $text.
Used by · 2
- add_object_page()Add a top-level menu page in the 'objects' section.
- add_utility_page()Add a top-level menu page in the 'utility' section.
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.
Signature, return type and hooks compared across 5 parsed releases.
About 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.