WP_User::has_cap( string $cap, mixed $args ): bool
- Since
- 2.0.0, 5.3.0
- Source
wp-includes/class-wp-user.php:776
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
- Scaling
- Scales with input
- Instructions
- 26–63
- Plugin surface
- 1 hook
- Called by
- 0
Reaches the database via get_post().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 70.
Third-party callbacks on 'user_has_cap' run inside this call, and their cost is not bounded by anything here.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
apply_filters()called directly - querycontent query
get_post()one call below WP_User::has_cap() - optionoption read or write
get_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 · 6 distinct outcomes
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.
| When | Instructions | Calls it makes |
|---|---|---|
!$cap && is_multisite() && is_super_admin() | 26 | map_meta_cap(), is_multisite(), is_super_admin() |
$cap && is_multisite() && is_super_admin() | 38 | __(), _deprecated_argument(), ->translate_level_to_cap(), map_meta_cap(), is_multisite(), is_super_admin() |
!$cap && !is_multisite() | 42–45 | map_meta_cap(), is_multisite(), apply_filters() |
!$cap && is_multisite() && !is_super_admin() | 48–51 | map_meta_cap(), is_multisite(), is_super_admin(), apply_filters() |
$cap && !is_multisite() | 54–57 | __(), _deprecated_argument(), ->translate_level_to_cap(), map_meta_cap(), is_multisite(), apply_filters() |
$cap && is_multisite() && !is_super_admin() | 60–63 | __(), _deprecated_argument(), ->translate_level_to_cap(), map_meta_cap(), is_multisite(), is_super_admin(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 70 | 26–63 | 7 | |
| 8.5 | 70 | 26–63 | 7 | |
| 8.4 | 70 | 26–63 | 7 | 5 fewer instructions than PHP 8.3 |
| 8.3 | 75 | 31–65 | 7 | |
| 8.2 | 75 | 31–65 | 7 | |
| 8.1 | 75 | 31–65 | 7 | 1 more instruction than PHP 7.4 |
| 7.4 | 74 | 30–64 | 7 |
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:
- apply_filters( user_has_cap )filterline 813 (+37 into the body)
Dynamically filter a user's capabilities.
Uses · 7
- _deprecated_argument()Marks a function argument as deprecated and inform when it has been used.
- __()Retrieves the translation of $text.
- map_meta_cap()Maps a capability to the primitive capabilities required of the given user to satisfy the capability being checked.
- is_multisite()Determines whether Multisite is enabled.
- is_super_admin()Determines whether user is a site admin.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- WP_User::translate_level_to_cap()Converts numeric level to level capability name.
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. foreach ( (array) $caps as $cap ) { if ( empty( $capabilities[ $cap ] ) ) { return false; } } return true; }Changelog
Introduced in 2.0.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
...$args parameter by adding it to the function signature.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 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.