user_can_for_site( int|WP_User $user, int $site_id, string $capability, mixed $args ): bool
- Since
- 6.7.0
- Source
wp-includes/capabilities.php:1045
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_can_for_site( $user->ID, $site_id, 'edit_posts' );
user_can_for_site( $user->ID, $site_id, 'edit_post', $post->ID );
user_can_for_site( $user->ID, $site_id, 'edit_post_meta', $post->ID, $meta_key );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
$userint|WP_User- User ID or object.
$site_idint- Site ID.
$capabilitystring- Capability name.
$argsmixed- Optional further parameters, typically starting with an object ID.
Return value
bool- Whether the user has the given capability.
Performance profile
How much work a call to user_can_for_site() 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
- Constant
- Instructions
- 11–47
- Plugin surface
- None
- Called by
- 0
Reaches the database via get_user_by().
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 49.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- querycontent query
get_user_by()one call below user_can_for_site() - hookthird-party callbacks
do_action()one call below user_can_for_site() - cacheobject cache
wp_cache_switch_to_blog()one call below user_can_for_site()
What one call costs · 20 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost user_can_for_site() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
is_object($user) && !empty($user) | 11–13 | none |
!is_object($user) && !empty($user) | 15–17 | get_userdata() |
is_object($user) && empty($user) | 20–22 | ->init() |
!is_object($user) && empty($user) | 24–26 | get_userdata(), ->init() |
is_object($user) && !empty($user) && !is_multisite() | 28 | is_multisite(), user_can() |
is_object($user) && !empty($user) && !is_multisite() | 30 | is_multisite(), user_can(), restore_current_blog() |
is_object($user) && !empty($user) && is_multisite() | 32 | is_multisite(), switch_to_blog(), user_can() |
!is_object($user) && !empty($user) && !is_multisite() | 32 | get_userdata(), is_multisite(), user_can() |
is_object($user) && !empty($user) && is_multisite() | 34 | is_multisite(), switch_to_blog(), user_can(), restore_current_blog() |
!is_object($user) && !empty($user) && !is_multisite() | 34 | get_userdata(), is_multisite(), user_can(), restore_current_blog() |
!is_object($user) && !empty($user) && is_multisite() | 36 | get_userdata(), is_multisite(), switch_to_blog(), user_can() |
is_object($user) && empty($user) && !is_multisite() | 37 | ->init(), is_multisite(), user_can() |
8 further outcomes, up to 47 instructions
!is_object($user) && !empty($user) && is_multisite() | 38 | get_userdata(), is_multisite(), switch_to_blog(), user_can(), restore_current_blog() |
is_object($user) && empty($user) && !is_multisite() | 39 | ->init(), is_multisite(), user_can(), restore_current_blog() |
is_object($user) && empty($user) && is_multisite() | 41 | ->init(), is_multisite(), switch_to_blog(), user_can() |
!is_object($user) && empty($user) && !is_multisite() | 41 | get_userdata(), ->init(), is_multisite(), user_can() |
is_object($user) && empty($user) && is_multisite() | 43 | ->init(), is_multisite(), switch_to_blog(), user_can(), restore_current_blog() |
!is_object($user) && empty($user) && !is_multisite() | 43 | get_userdata(), ->init(), is_multisite(), user_can(), restore_current_blog() |
!is_object($user) && empty($user) && is_multisite() | 45 | get_userdata(), ->init(), is_multisite(), switch_to_blog(), user_can() |
!is_object($user) && empty($user) && is_multisite() | 47 | get_userdata(), ->init(), is_multisite(), switch_to_blog(), user_can(), restore_current_blog() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 48 | 11–46 | 6 | 1 fewer instruction than PHP 8.5 |
| 8.5 | 49 | 11–47 | 6 | |
| 8.4 | 49 | 11–47 | 6 | 2 fewer instructions than PHP 8.3 |
| 8.3 | 51 | 13–49 | 6 | |
| 8.2 | 51 | 13–49 | 6 | |
| 8.1 | 51 | 13–49 | 6 | 1 more instruction than PHP 7.4 |
| 7.4 | 50 | 13–48 | 6 |
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 · 7
- get_userdata()Retrieves user info by user ID.
- is_multisite()Determines whether Multisite is enabled.
- switch_to_blog()Switches the current blog.
- user_can()Returns whether a particular user has the specified capability.
- restore_current_blog()Restores the current blog, after calling switch_to_blog().
- WP_User::__construct()Constructor.
- stdClass::__construct()
Source code
function user_can_for_site( $user, $site_id, $capability, ...$args ) { if ( ! is_object( $user ) ) { $user = get_userdata( $user ); } if ( empty( $user ) ) { // User is logged out, create anonymous user object. $user = new WP_User( 0 ); $user->init( new stdClass() ); } // Check if the blog ID is valid. if ( ! is_numeric( $site_id ) || $site_id <= 0 ) { return false; } $switched = is_multisite() ? switch_to_blog( $site_id ) : false; $can = user_can( $user->ID, $capability, ...$args ); if ( $switched ) { restore_current_blog(); } return $can;}Changelog
Introduced in 6.7.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 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.