wppaste
WordPress

map_meta_cap( string $cap, int $user_id, mixed $args ): string[]

Since
2.0.0, 4.9.6, 5.1.0, 5.2.0, 5.3.0, 5.7.0, 6.7.0
Source
wp-includes/capabilities.php:45
Maps a capability to the primitive capabilities required of the given user to satisfy the capability being checked.

Description

This function also accepts an ID of an object to map against if the capability is a meta capability. Meta capabilities such as edit_post and edit_user are capabilities used by this function to map to primitive capabilities that a user or role requires, such as edit_posts and edit_others_posts.

Example usage:

map_meta_cap( 'edit_posts', $user->ID );
map_meta_cap( 'edit_post', $user->ID, $post->ID );
map_meta_cap( 'edit_post_meta', $user->ID, $post->ID, $meta_key );

This function does not check whether the user has the required capabilities, it just returns what the required capabilities are.

Compatibility

WordPress
since 6.7.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

$capstring
Capability being checked.
$user_idint
User ID.
$argsmixed
Optional further parameters, typically starting with an object ID.

Return value

string[]
Primitive capabilities required of the user.

Performance profile

How much work a call to map_meta_cap() 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
Heavy

Reaches the database via get_post().

Scaling
Constant

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

Instructions
15–102

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

Plugin surface
4 hooks

Third-party callbacks on 'auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype}', 'auth_{$object_type}_meta_{$meta_key}', 'auth_{$object_type}_{$object_subtype}_meta_{$meta_key}' run inside this call, and their cost is not bounded by anything here.

Called by
3

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

What it touches

  • querycontent queryget_post()called directly
  • optionoption read or writeget_option()called directly
  • hookthird-party callbacksapply_filters()called directly
  • cacheobject cachewp_cache_get()one call below map_meta_cap()
  • serializeserialisationmaybe_unserialize()one call below map_meta_cap()

Further down the call graph this can also reach transient. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 45 distinct outcomes

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

WhenInstructionsCalls it makes
always15–23apply_filters()
!is_multisite()20–28is_multisite(), apply_filters()
!wp_is_file_mod_allowed()21–23wp_is_file_mod_allowed(), apply_filters()
isset($args)24–27get_post(), apply_filters()
isset($args)24get_comment(), apply_filters()
is_multisite()24–27is_multisite(), is_super_admin(), apply_filters()
isset($args) && $user_id === false25is_super_admin(), apply_filters()
wp_is_file_mod_allowed() && !is_multisite()25–27wp_is_file_mod_allowed(), is_multisite(), apply_filters()
wp_is_file_mod_allowed() && is_multisite()28–31wp_is_file_mod_allowed(), is_multisite(), is_super_admin(), apply_filters()
isset($args) && empty($object_subtype)31explode(), get_object_subtype(), apply_filters()
!$user_id && is_multisite()31–39is_multisite(), is_super_admin(), user_can(), apply_filters()
!isset($args)32–35__(), sprintf(), _doing_it_wrong(), apply_filters()
33 further outcomes, up to 102 instructions
isset($args)34get_post(), get_option(), apply_filters()
isset($args)34–44get_post(), get_post_type_object(), apply_filters()
isset($args)34get_post(), get_post(), apply_filters()
!$user_id && is_multisite() && $cap === "edit_user"34–40is_multisite(), is_super_admin(), is_super_admin(), apply_filters()
isset($args)35–38get_comment(), get_post(), map_meta_cap(), apply_filters()
!$user_id && is_multisite() && !is_super_admin() && $cap === "edit_user"39–45is_multisite(), is_super_admin(), is_super_admin(), user_can(), apply_filters()
isset($args) && !empty($object_subtype)40–41explode(), get_object_subtype(), map_meta_cap(), apply_filters()
!isset($args)40–44explode(), __(), sprintf(), _doing_it_wrong(), apply_filters()
isset($args)41get_post(), get_option(), get_option(), apply_filters()
isset($args) && !$post_type47–51get_post(), get_post(), get_post_type_object(), apply_filters()
isset($args) && $post_type49–57get_post(), get_post_type_object(), get_post_status(), get_post_status_object(), apply_filters()
isset($args)50–53get_post(), get_post_type_object(), __(), sprintf(), _doing_it_wrong(), apply_filters()
isset($args) && $post_type53–61get_post(), get_post_type_object(), get_option(), apply_filters()
isset($args) && !$post_type54–58get_post(), get_option(), get_option(), get_post_type_object(), apply_filters()
isset($args) && $post_type56–64get_post(), get_post(), get_post_type_object(), get_post_status(), get_post_status_object(), apply_filters()
isset($args) && $post_type && !$status_obj57–61get_post(), get_post_type_object(), get_post_status(), get_post_status_object(), map_meta_cap(), apply_filters()
isset($args) && $post_type60–68get_post(), get_post(), get_post_type_object(), get_option(), apply_filters()
isset($args)60get_post(), get_post(), get_post_type_object(), __(), sprintf(), _doing_it_wrong(), apply_filters()
isset($args) && $post_type63–71get_post(), get_post_type_object(), get_option(), map_meta_cap(), array_merge(), apply_filters()
isset($args) && $post_type && !$status_obj64–68get_post(), get_post(), get_post_type_object(), get_post_status(), get_post_status_object(), map_meta_cap(), apply_filters()
isset($args) && $post_type65get_post(), get_post_type_object(), get_post_status(), get_post_status_object(), __(), get_post_status(), sprintf(), _doing_it_wrong(), apply_filters()
isset($args) && $post_type67–75get_post(), get_option(), get_option(), get_post_type_object(), get_option(), apply_filters()
isset($args)67get_post(), get_option(), get_option(), get_post_type_object(), __(), sprintf(), _doing_it_wrong(), apply_filters()
isset($args) && $post_type && $post && $user_id === false68get_post(), get_post_type_object(), get_post_meta(), get_option(), apply_filters()
isset($args) && $post_type70–78get_post(), get_post(), get_post_type_object(), get_option(), map_meta_cap(), array_merge(), apply_filters()
isset($args) && $post_type72get_post(), get_post(), get_post_type_object(), get_post_status(), get_post_status_object(), __(), get_post_status(), sprintf(), _doing_it_wrong(), apply_filters()
isset($args) && $post_type && $post && $user_id === false75get_post(), get_post(), get_post_type_object(), get_post_meta(), get_option(), apply_filters()
isset($args) && $post_type77–85get_post(), get_option(), get_option(), get_post_type_object(), get_option(), map_meta_cap(), array_merge(), apply_filters()
isset($args) && $post_type && $post && $user_id === false78get_post(), get_post_type_object(), get_post_meta(), get_option(), map_meta_cap(), array_merge(), apply_filters()
isset($args) && $post_type && $post && $user_id === false82get_post(), get_option(), get_option(), get_post_type_object(), get_post_meta(), get_option(), apply_filters()
isset($args) && $post_type && $post && $user_id === false85get_post(), get_post(), get_post_type_object(), get_post_meta(), get_option(), map_meta_cap(), array_merge(), apply_filters()
isset($args) && $post_type && $post && $user_id === false92get_post(), get_option(), get_option(), get_post_type_object(), get_post_meta(), get_option(), map_meta_cap(), array_merge(), apply_filters()
isset($args) && !empty($object_subtype)95–102explode(), get_object_subtype(), map_meta_cap(), is_protected_meta(), has_filter(), apply_filters(), apply_filters()

This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev134615–102211
8.5134615–102211
8.4134615–1022113 fewer instructions than PHP 8.3
8.3134915–102211
8.2134915–1022111 more instruction than PHP 8.1
8.1134815–1022111 more instruction than PHP 7.4
7.4134715–102211

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.

Hooks and filters fired · 4

4 hooks fire while map_meta_cap() runs, in this order:

  1. apply_filters( auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype} )filterline 497 (+452 into the body)

    Filters whether the user is allowed to edit a specific meta key of a specific object type and subtype.

  2. apply_filters( auth_{$object_type}_meta_{$meta_key} )filterline 518 (+473 into the body)

    Filters whether the user is allowed to edit a specific meta key of a specific object type.

  3. do_action( auth_{$object_type}_{$object_subtype}_meta_{$meta_key} )filter_deprecatedline 542 (+497 into the body)

    Filters whether the user is allowed to edit meta for specific object types/subtypes.

  4. apply_filters( map_meta_cap )filterline 879 (+834 into the body)

    Filters the primitive capabilities required of the given user to satisfy the capability being checked.

Uses · 24

Show all 24

Used by · 3

Source code

function map_meta_cap( $cap, $user_id, ...$args ) {	$caps = array(); 	switch ( $cap ) {		case 'remove_user':			// In multisite the user must be a super admin to remove themselves.			if ( isset( $args[0] ) && $user_id === (int) $args[0] && ! is_super_admin( $user_id ) ) {				$caps[] = 'do_not_allow';			} else {				$caps[] = 'remove_users';			}			break;		case 'promote_user':		case 'add_users':			$caps[] = 'promote_users';			break;		case 'edit_user':		case 'edit_users':			// Non-existent users can't edit users, not even themselves.			if ( $user_id < 1 ) {				$caps[] = 'do_not_allow';				break;			} 			// Allow user to edit themselves.			if ( 'edit_user' === $cap && isset( $args[0] ) && $user_id === (int) $args[0] ) {				break;			} 			// In multisite the user must have manage_network_users caps. If editing a super admin, the user must be a super admin.			if ( is_multisite() && ( ( ! is_super_admin( $user_id ) && 'edit_user' === $cap && is_super_admin( $args[0] ) ) || ! user_can( $user_id, 'manage_network_users' ) ) ) {				$caps[] = 'do_not_allow';			} else {				$caps[] = 'edit_users'; // edit_user maps to edit_users.			}			break;		case 'delete_post':		case 'delete_page':			if ( ! isset( $args[0] ) ) {				if ( 'delete_post' === $cap ) {					/* translators: %s: Capability name. */					$message = __( 'When checking for the %s capability, you must always check it against a specific post.' );				} else {					/* translators: %s: Capability name. */					$message = __( 'When checking for the %s capability, you must always check it against a specific page.' );				} 				_doing_it_wrong(					__FUNCTION__,					sprintf( $message, '<code>' . $cap . '</code>' ),					'6.1.0'				); 				$caps[] = 'do_not_allow';				break;			} 			$post = get_post( $args[0] );			if ( ! $post ) {				$caps[] = 'do_not_allow';				break;			} 			if ( 'revision' === $post->post_type ) {				$caps[] = 'do_not_allow';				break;			} 			if ( (int) get_option( 'page_for_posts' ) === $post->ID				|| (int) get_option( 'page_on_front' ) === $post->ID			) {				$caps[] = 'manage_options';				break;			} 			$post_type = get_post_type_object( $post->post_type );			if ( ! $post_type ) {				/* translators: 1: Post type, 2: Capability name. */				$message = __( 'The post type %1$s is not registered, so it may not be reliable to check the capability %2$s against a post of that type.' );

Changelog

Introduced in 2.0.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.

6.7.0
Added the edit_block_binding capability.from the docblock
5.7.0
Added the create_app_password, list_app_passwords, read_app_password, edit_app_password, delete_app_passwords, delete_app_password, and update_https capabilities.from the docblock
5.3.0
Formalized the existing and already documented ...$args parameter by adding it to the function signature.from the docblock
5.2.0
Added the resume_plugin and resume_theme capabilities.from the docblock
5.1.0
Added the update_php capability.from the docblock
4.9.6
Added the export_others_personal_data, erase_others_personal_data, and manage_privacy_options capabilities.from the docblock
2.0.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/capabilities.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.