wppaste
WordPress

WP_User::has_cap( string $cap, mixed $args ): bool

Since
2.0.0, 5.3.0
Source
wp-includes/class-wp-user.php:787
Returns whether the user has the specified capability.

Description

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

Example usage:

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

While checking against a role in place of a capability is supported in part, this practice is discouraged as it may produce unreliable results.

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

$capstring
Capability name.
$argsmixed
Optional further parameters, typically starting with an object ID.

Return value

bool
Whether the user has the given capability, or, if an object ID is passed, whether the user has the given capability for that object.

Performance profile

How much work a call to WP_User::has_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
6

Executed per call on PHP 8.5. The body compiles to 75.

Plugin surface
1 hook

Third-party callbacks on 'user_has_cap' run inside this call, and their cost is not bounded by anything here.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • querycontent queryget_post()one call below WP_User::has_cap()
  • optionoption read or writeget_option()one call below WP_User::has_cap()

Further down the call graph this can also reach 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 · 1 distinct outcome

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

WhenInstructionsCalls it makes
always6none

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev7564
8.57564
8.475645 fewer instructions than PHP 8.3
8.38064
8.28064
8.180647 more instructions than PHP 7.4
7.47330–654

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

One hook fires while WP_User::has_cap() runs, in this order:

  1. apply_filters( user_has_cap )filterline 824 (+37 into the body)

    Dynamically filter a user's capabilities.

Uses · 8

Source code

	public function has_cap( $cap, ...$args ) {		if ( is_numeric( $cap ) ) {			_deprecated_argument( __FUNCTION__, '2.0.0', __( 'Usage of user levels is deprecated. Use capabilities instead.' ) );			$cap = $this->translate_level_to_cap( $cap );		} 		$caps = map_meta_cap( $cap, $this->ID, ...$args ); 		// Multisite super admin has all caps by definition, Unless specifically denied.		if ( is_multisite() && is_super_admin( $this->ID ) ) {			if ( in_array( 'do_not_allow', $caps, true ) ) {				return false;			}			return true;		} 		// Maintain BC for the argument passed to the "user_has_cap" filter.		$args = array_merge( array( $cap, $this->ID ), $args ); 		/**		 * Dynamically filter a user's capabilities.		 *		 * @since 2.0.0		 * @since 3.7.0 Added the `$user` parameter.		 *		 * @param bool[]   $allcaps Array of key/value pairs where keys represent a capability name		 *                          and boolean values represent whether the user has that capability.		 * @param string[] $caps    Required primitive capabilities for the requested capability.		 * @param array    $args {		 *     Arguments that accompany the requested capability check.		 *		 *     @type string    $0 Requested capability.		 *     @type int       $1 Concerned user ID.		 *     @type mixed  ...$2 Optional second and further parameters, typically object ID.		 * }		 * @param WP_User  $user    The user object.		 */		$capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args, $this ); 		// Everyone is allowed to exist.		$capabilities['exist'] = true; 		// Nobody is allowed to do things they are not allowed to do.		unset( $capabilities['do_not_allow'] ); 		// Must have ALL requested caps.		return array_all( (array) $caps, fn( $cap, $key ) => ! empty( $capabilities[ $cap ] ) );	}

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.

5.3.0
Formalized the existing and already documented ...$args parameter by adding it to the function signature.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/class-wp-user.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.