current_user_can_for_blog

The timeline below displays how wordpress function current_user_can_for_blog has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 5.8

/**
 * Returns whether the current user has the specified capability for a given site.
 *
 * 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:
 *
 *     current_user_can_for_blog( $blog_id, 'edit_posts' );
 *     current_user_can_for_blog( $blog_id, 'edit_post', $post->ID );
 *     current_user_can_for_blog( $blog_id, 'edit_post_meta', $post->ID, $meta_key );
 *
 * @since 3.0.0
 * @since 5.3.0 Formalized the existing and already documented `...$args` parameter
 *              by adding it to the function signature.
 * @since 5.8.0 Wraps current_user_can() after switching to blog.
 *
 * @param int    $blog_id    Site ID.
 * @param string $capability Capability name.
 * @param mixed  ...$args    Optional further parameters, typically starting with an object ID.
 * @return bool Whether the user has the given capability.
 */
function current_user_can_for_blog($blog_id, $capability, ...$args)
{
    $switched = is_multisite() ? switch_to_blog($blog_id) : false;
    $can = current_user_can($capability, ...$args);
    if ($switched) {
        restore_current_blog();
    }
    return $can;
}

WordPress Version: 5.3

/**
 * Returns whether the current user has the specified capability for a given site.
 *
 * 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:
 *
 *     current_user_can_for_blog( $blog_id, 'edit_posts' );
 *     current_user_can_for_blog( $blog_id, 'edit_post', $post->ID );
 *     current_user_can_for_blog( $blog_id, 'edit_post_meta', $post->ID, $meta_key );
 *
 * @since 3.0.0
 * @since 5.3.0 Formalized the existing and already documented `...$args` parameter
 *              by adding it to the function signature.
 *
 * @param int    $blog_id    Site ID.
 * @param string $capability Capability name.
 * @param mixed  ...$args    Optional further parameters, typically starting with an object ID.
 * @return bool Whether the user has the given capability.
 */
function current_user_can_for_blog($blog_id, $capability, ...$args)
{
    $switched = is_multisite() ? switch_to_blog($blog_id) : false;
    $current_user = wp_get_current_user();
    if (empty($current_user)) {
        if ($switched) {
            restore_current_blog();
        }
        return false;
    }
    $can = $current_user->has_cap($capability, ...$args);
    if ($switched) {
        restore_current_blog();
    }
    return $can;
}

WordPress Version: 4.9

/**
 * Whether the current user has a specific capability for a given site.
 *
 * @since 3.0.0
 *
 * @param int    $blog_id    Site ID.
 * @param string $capability Capability name.
 * @return bool Whether the user has the given capability.
 */
function current_user_can_for_blog($blog_id, $capability)
{
    $switched = is_multisite() ? switch_to_blog($blog_id) : false;
    $current_user = wp_get_current_user();
    if (empty($current_user)) {
        if ($switched) {
            restore_current_blog();
        }
        return false;
    }
    $args = array_slice(func_get_args(), 2);
    $args = array_merge(array($capability), $args);
    $can = call_user_func_array(array($current_user, 'has_cap'), $args);
    if ($switched) {
        restore_current_blog();
    }
    return $can;
}

WordPress Version: 4.5

/**
 * Whether current user has a capability or role for a given site.
 *
 * @since 3.0.0
 *
 * @param int    $blog_id    Site ID.
 * @param string $capability Capability or role name.
 * @return bool
 */
function current_user_can_for_blog($blog_id, $capability)
{
    $switched = is_multisite() ? switch_to_blog($blog_id) : false;
    $current_user = wp_get_current_user();
    if (empty($current_user)) {
        if ($switched) {
            restore_current_blog();
        }
        return false;
    }
    $args = array_slice(func_get_args(), 2);
    $args = array_merge(array($capability), $args);
    $can = call_user_func_array(array($current_user, 'has_cap'), $args);
    if ($switched) {
        restore_current_blog();
    }
    return $can;
}

WordPress Version: .10

/**
 * Whether current user has a capability or role for a given blog.
 *
 * @since 3.0.0
 *
 * @param int $blog_id Blog ID
 * @param string $capability Capability or role name.
 * @return bool
 */
function current_user_can_for_blog($blog_id, $capability)
{
    $switched = is_multisite() ? switch_to_blog($blog_id) : false;
    $current_user = wp_get_current_user();
    if (empty($current_user)) {
        if ($switched) {
            restore_current_blog();
        }
        return false;
    }
    $args = array_slice(func_get_args(), 2);
    $args = array_merge(array($capability), $args);
    $can = call_user_func_array(array($current_user, 'has_cap'), $args);
    if ($switched) {
        restore_current_blog();
    }
    return $can;
}

WordPress Version: 4.1

/**
 * Whether current user has a capability or role for a given blog.
 *
 * @since 3.0.0
 *
 * @param int $blog_id Blog ID
 * @param string $capability Capability or role name.
 * @return bool
 */
function current_user_can_for_blog($blog_id, $capability)
{
    if (is_multisite()) {
        switch_to_blog($blog_id);
    }
    $current_user = wp_get_current_user();
    if (empty($current_user)) {
        return false;
    }
    $args = array_slice(func_get_args(), 2);
    $args = array_merge(array($capability), $args);
    $can = call_user_func_array(array($current_user, 'has_cap'), $args);
    if (is_multisite()) {
        restore_current_blog();
    }
    return $can;
}

WordPress Version: .10

/**
 * Whether current user has a capability or role for a given blog.
 *
 * @since 3.0.0
 *
 * @param int $blog_id Blog ID
 * @param string $capability Capability or role name.
 * @return bool
 */
function current_user_can_for_blog($blog_id, $capability)
{
    $switched = is_multisite() ? switch_to_blog($blog_id) : false;
    $current_user = wp_get_current_user();
    if (empty($current_user)) {
        if ($switched) {
            restore_current_blog();
        }
        return false;
    }
    $args = array_slice(func_get_args(), 2);
    $args = array_merge(array($capability), $args);
    $can = call_user_func_array(array($current_user, 'has_cap'), $args);
    if ($switched) {
        restore_current_blog();
    }
    return $can;
}

WordPress Version: 4.0

/**
 * Whether current user has a capability or role for a given blog.
 *
 * @since 3.0.0
 *
 * @param int $blog_id Blog ID
 * @param string $capability Capability or role name.
 * @return bool
 */
function current_user_can_for_blog($blog_id, $capability)
{
    if (is_multisite()) {
        switch_to_blog($blog_id);
    }
    $current_user = wp_get_current_user();
    if (empty($current_user)) {
        return false;
    }
    $args = array_slice(func_get_args(), 2);
    $args = array_merge(array($capability), $args);
    $can = call_user_func_array(array($current_user, 'has_cap'), $args);
    if (is_multisite()) {
        restore_current_blog();
    }
    return $can;
}

WordPress Version: 9.4

/**
 * Whether current user has a capability or role for a given blog.
 *
 * @since 3.0.0
 *
 * @param int $blog_id Blog ID
 * @param string $capability Capability or role name.
 * @return bool
 */
function current_user_can_for_blog($blog_id, $capability)
{
    $switched = is_multisite() ? switch_to_blog($blog_id) : false;
    $current_user = wp_get_current_user();
    if (empty($current_user)) {
        if ($switched) {
            restore_current_blog();
        }
        return false;
    }
    $args = array_slice(func_get_args(), 2);
    $args = array_merge(array($capability), $args);
    $can = call_user_func_array(array($current_user, 'has_cap'), $args);
    if ($switched) {
        restore_current_blog();
    }
    return $can;
}

WordPress Version: 9.2

/**
 * Whether current user has a capability or role for a given blog.
 *
 * @since 3.0.0
 *
 * @param int $blog_id Blog ID
 * @param string $capability Capability or role name.
 * @return bool
 */
function current_user_can_for_blog($blog_id, $capability)
{
    if (is_multisite()) {
        switch_to_blog($blog_id);
    }
    $current_user = wp_get_current_user();
    if (empty($current_user)) {
        return false;
    }
    $args = array_slice(func_get_args(), 2);
    $args = array_merge(array($capability), $args);
    $can = call_user_func_array(array($current_user, 'has_cap'), $args);
    if (is_multisite()) {
        restore_current_blog();
    }
    return $can;
}

WordPress Version: .10

/**
 * Whether current user has a capability or role for a given blog.
 *
 * @since 3.0.0
 *
 * @param int $blog_id Blog ID
 * @param string $capability Capability or role name.
 * @return bool
 */
function current_user_can_for_blog($blog_id, $capability)
{
    $switched = is_multisite() ? switch_to_blog($blog_id) : false;
    $current_user = wp_get_current_user();
    if (empty($current_user)) {
        if ($switched) {
            restore_current_blog();
        }
        return false;
    }
    $args = array_slice(func_get_args(), 2);
    $args = array_merge(array($capability), $args);
    $can = call_user_func_array(array($current_user, 'has_cap'), $args);
    if ($switched) {
        restore_current_blog();
    }
    return $can;
}

WordPress Version: 3.9

/**
 * Whether current user has a capability or role for a given blog.
 *
 * @since 3.0.0
 *
 * @param int $blog_id Blog ID
 * @param string $capability Capability or role name.
 * @return bool
 */
function current_user_can_for_blog($blog_id, $capability)
{
    if (is_multisite()) {
        switch_to_blog($blog_id);
    }
    $current_user = wp_get_current_user();
    if (empty($current_user)) {
        return false;
    }
    $args = array_slice(func_get_args(), 2);
    $args = array_merge(array($capability), $args);
    $can = call_user_func_array(array($current_user, 'has_cap'), $args);
    if (is_multisite()) {
        restore_current_blog();
    }
    return $can;
}

WordPress Version: 8.6

/**
 * Whether current user has a capability or role for a given blog.
 *
 * @since 3.0.0
 *
 * @param int $blog_id Blog ID
 * @param string $capability Capability or role name.
 * @return bool
 */
function current_user_can_for_blog($blog_id, $capability)
{
    $switched = is_multisite() ? switch_to_blog($blog_id) : false;
    $current_user = wp_get_current_user();
    if (empty($current_user)) {
        if ($switched) {
            restore_current_blog();
        }
        return false;
    }
    $args = array_slice(func_get_args(), 2);
    $args = array_merge(array($capability), $args);
    $can = call_user_func_array(array($current_user, 'has_cap'), $args);
    if ($switched) {
        restore_current_blog();
    }
    return $can;
}

WordPress Version: 8.4

/**
 * Whether current user has a capability or role for a given blog.
 *
 * @since 3.0.0
 *
 * @param int $blog_id Blog ID
 * @param string $capability Capability or role name.
 * @return bool
 */
function current_user_can_for_blog($blog_id, $capability)
{
    if (is_multisite()) {
        switch_to_blog($blog_id);
    }
    $current_user = wp_get_current_user();
    if (empty($current_user)) {
        return false;
    }
    $args = array_slice(func_get_args(), 2);
    $args = array_merge(array($capability), $args);
    $can = call_user_func_array(array($current_user, 'has_cap'), $args);
    if (is_multisite()) {
        restore_current_blog();
    }
    return $can;
}

WordPress Version: .30

/**
 * Whether current user has a capability or role for a given blog.
 *
 * @since 3.0.0
 *
 * @param int $blog_id Blog ID
 * @param string $capability Capability or role name.
 * @return bool
 */
function current_user_can_for_blog($blog_id, $capability)
{
    $switched = is_multisite() ? switch_to_blog($blog_id) : false;
    $current_user = wp_get_current_user();
    if (empty($current_user)) {
        if ($switched) {
            restore_current_blog();
        }
        return false;
    }
    $args = array_slice(func_get_args(), 2);
    $args = array_merge(array($capability), $args);
    $can = call_user_func_array(array($current_user, 'has_cap'), $args);
    if ($switched) {
        restore_current_blog();
    }
    return $can;
}

WordPress Version: 8.3

/**
 * Whether current user has a capability or role for a given blog.
 *
 * @since 3.0.0
 *
 * @param int $blog_id Blog ID
 * @param string $capability Capability or role name.
 * @return bool
 */
function current_user_can_for_blog($blog_id, $capability)
{
    if (is_multisite()) {
        switch_to_blog($blog_id);
    }
    $current_user = wp_get_current_user();
    if (empty($current_user)) {
        return false;
    }
    $args = array_slice(func_get_args(), 2);
    $args = array_merge(array($capability), $args);
    $can = call_user_func_array(array($current_user, 'has_cap'), $args);
    if (is_multisite()) {
        restore_current_blog();
    }
    return $can;
}

WordPress Version: .20

/**
 * Whether current user has a capability or role for a given blog.
 *
 * @since 3.0.0
 *
 * @param int $blog_id Blog ID
 * @param string $capability Capability or role name.
 * @return bool
 */
function current_user_can_for_blog($blog_id, $capability)
{
    $switched = is_multisite() ? switch_to_blog($blog_id) : false;
    $current_user = wp_get_current_user();
    if (empty($current_user)) {
        if ($switched) {
            restore_current_blog();
        }
        return false;
    }
    $args = array_slice(func_get_args(), 2);
    $args = array_merge(array($capability), $args);
    $can = call_user_func_array(array($current_user, 'has_cap'), $args);
    if ($switched) {
        restore_current_blog();
    }
    return $can;
}

WordPress Version: 8.2

/**
 * Whether current user has a capability or role for a given blog.
 *
 * @since 3.0.0
 *
 * @param int $blog_id Blog ID
 * @param string $capability Capability or role name.
 * @return bool
 */
function current_user_can_for_blog($blog_id, $capability)
{
    if (is_multisite()) {
        switch_to_blog($blog_id);
    }
    $current_user = wp_get_current_user();
    if (empty($current_user)) {
        return false;
    }
    $args = array_slice(func_get_args(), 2);
    $args = array_merge(array($capability), $args);
    $can = call_user_func_array(array($current_user, 'has_cap'), $args);
    if (is_multisite()) {
        restore_current_blog();
    }
    return $can;
}

WordPress Version: .10

/**
 * Whether current user has a capability or role for a given blog.
 *
 * @since 3.0.0
 *
 * @param int $blog_id Blog ID
 * @param string $capability Capability or role name.
 * @return bool
 */
function current_user_can_for_blog($blog_id, $capability)
{
    $switched = is_multisite() ? switch_to_blog($blog_id) : false;
    $current_user = wp_get_current_user();
    if (empty($current_user)) {
        if ($switched) {
            restore_current_blog();
        }
        return false;
    }
    $args = array_slice(func_get_args(), 2);
    $args = array_merge(array($capability), $args);
    $can = call_user_func_array(array($current_user, 'has_cap'), $args);
    if ($switched) {
        restore_current_blog();
    }
    return $can;
}

WordPress Version: 3.8

/**
 * Whether current user has a capability or role for a given blog.
 *
 * @since 3.0.0
 *
 * @param int $blog_id Blog ID
 * @param string $capability Capability or role name.
 * @return bool
 */
function current_user_can_for_blog($blog_id, $capability)
{
    if (is_multisite()) {
        switch_to_blog($blog_id);
    }
    $current_user = wp_get_current_user();
    if (empty($current_user)) {
        return false;
    }
    $args = array_slice(func_get_args(), 2);
    $args = array_merge(array($capability), $args);
    $can = call_user_func_array(array($current_user, 'has_cap'), $args);
    if (is_multisite()) {
        restore_current_blog();
    }
    return $can;
}

WordPress Version: 7.6

/**
 * Whether current user has a capability or role for a given blog.
 *
 * @since 3.0.0
 *
 * @param int $blog_id Blog ID
 * @param string $capability Capability or role name.
 * @return bool
 */
function current_user_can_for_blog($blog_id, $capability)
{
    $switched = is_multisite() ? switch_to_blog($blog_id) : false;
    $current_user = wp_get_current_user();
    if (empty($current_user)) {
        if ($switched) {
            restore_current_blog();
        }
        return false;
    }
    $args = array_slice(func_get_args(), 2);
    $args = array_merge(array($capability), $args);
    $can = call_user_func_array(array($current_user, 'has_cap'), $args);
    if ($switched) {
        restore_current_blog();
    }
    return $can;
}

WordPress Version: 7.5

/**
 * Whether current user has a capability or role for a given blog.
 *
 * @since 3.0.0
 *
 * @param int $blog_id Blog ID
 * @param string $capability Capability or role name.
 * @return bool
 */
function current_user_can_for_blog($blog_id, $capability)
{
    if (is_multisite()) {
        switch_to_blog($blog_id);
    }
    $current_user = wp_get_current_user();
    if (empty($current_user)) {
        return false;
    }
    $args = array_slice(func_get_args(), 2);
    $args = array_merge(array($capability), $args);
    $can = call_user_func_array(array($current_user, 'has_cap'), $args);
    if (is_multisite()) {
        restore_current_blog();
    }
    return $can;
}

WordPress Version: .40

/**
 * Whether current user has a capability or role for a given blog.
 *
 * @since 3.0.0
 *
 * @param int $blog_id Blog ID
 * @param string $capability Capability or role name.
 * @return bool
 */
function current_user_can_for_blog($blog_id, $capability)
{
    $switched = is_multisite() ? switch_to_blog($blog_id) : false;
    $current_user = wp_get_current_user();
    if (empty($current_user)) {
        if ($switched) {
            restore_current_blog();
        }
        return false;
    }
    $args = array_slice(func_get_args(), 2);
    $args = array_merge(array($capability), $args);
    $can = call_user_func_array(array($current_user, 'has_cap'), $args);
    if ($switched) {
        restore_current_blog();
    }
    return $can;
}

WordPress Version: 7.4

/**
 * Whether current user has a capability or role for a given blog.
 *
 * @since 3.0.0
 *
 * @param int $blog_id Blog ID
 * @param string $capability Capability or role name.
 * @return bool
 */
function current_user_can_for_blog($blog_id, $capability)
{
    if (is_multisite()) {
        switch_to_blog($blog_id);
    }
    $current_user = wp_get_current_user();
    if (empty($current_user)) {
        return false;
    }
    $args = array_slice(func_get_args(), 2);
    $args = array_merge(array($capability), $args);
    $can = call_user_func_array(array($current_user, 'has_cap'), $args);
    if (is_multisite()) {
        restore_current_blog();
    }
    return $can;
}

WordPress Version: .30

/**
 * Whether current user has a capability or role for a given blog.
 *
 * @since 3.0.0
 *
 * @param int $blog_id Blog ID
 * @param string $capability Capability or role name.
 * @return bool
 */
function current_user_can_for_blog($blog_id, $capability)
{
    $switched = is_multisite() ? switch_to_blog($blog_id) : false;
    $current_user = wp_get_current_user();
    if (empty($current_user)) {
        if ($switched) {
            restore_current_blog();
        }
        return false;
    }
    $args = array_slice(func_get_args(), 2);
    $args = array_merge(array($capability), $args);
    $can = call_user_func_array(array($current_user, 'has_cap'), $args);
    if ($switched) {
        restore_current_blog();
    }
    return $can;
}

WordPress Version: 7.3

/**
 * Whether current user has a capability or role for a given blog.
 *
 * @since 3.0.0
 *
 * @param int $blog_id Blog ID
 * @param string $capability Capability or role name.
 * @return bool
 */
function current_user_can_for_blog($blog_id, $capability)
{
    if (is_multisite()) {
        switch_to_blog($blog_id);
    }
    $current_user = wp_get_current_user();
    if (empty($current_user)) {
        return false;
    }
    $args = array_slice(func_get_args(), 2);
    $args = array_merge(array($capability), $args);
    $can = call_user_func_array(array($current_user, 'has_cap'), $args);
    if (is_multisite()) {
        restore_current_blog();
    }
    return $can;
}

WordPress Version: .20

/**
 * Whether current user has a capability or role for a given blog.
 *
 * @since 3.0.0
 *
 * @param int $blog_id Blog ID
 * @param string $capability Capability or role name.
 * @return bool
 */
function current_user_can_for_blog($blog_id, $capability)
{
    $switched = is_multisite() ? switch_to_blog($blog_id) : false;
    $current_user = wp_get_current_user();
    if (empty($current_user)) {
        if ($switched) {
            restore_current_blog();
        }
        return false;
    }
    $args = array_slice(func_get_args(), 2);
    $args = array_merge(array($capability), $args);
    $can = call_user_func_array(array($current_user, 'has_cap'), $args);
    if ($switched) {
        restore_current_blog();
    }
    return $can;
}

WordPress Version: 7.2

/**
 * Whether current user has a capability or role for a given blog.
 *
 * @since 3.0.0
 *
 * @param int $blog_id Blog ID
 * @param string $capability Capability or role name.
 * @return bool
 */
function current_user_can_for_blog($blog_id, $capability)
{
    if (is_multisite()) {
        switch_to_blog($blog_id);
    }
    $current_user = wp_get_current_user();
    if (empty($current_user)) {
        return false;
    }
    $args = array_slice(func_get_args(), 2);
    $args = array_merge(array($capability), $args);
    $can = call_user_func_array(array($current_user, 'has_cap'), $args);
    if (is_multisite()) {
        restore_current_blog();
    }
    return $can;
}

WordPress Version: .10

/**
 * Whether current user has a capability or role for a given blog.
 *
 * @since 3.0.0
 *
 * @param int $blog_id Blog ID
 * @param string $capability Capability or role name.
 * @return bool
 */
function current_user_can_for_blog($blog_id, $capability)
{
    $switched = is_multisite() ? switch_to_blog($blog_id) : false;
    $current_user = wp_get_current_user();
    if (empty($current_user)) {
        if ($switched) {
            restore_current_blog();
        }
        return false;
    }
    $args = array_slice(func_get_args(), 2);
    $args = array_merge(array($capability), $args);
    $can = call_user_func_array(array($current_user, 'has_cap'), $args);
    if ($switched) {
        restore_current_blog();
    }
    return $can;
}

WordPress Version: 3.7

/**
 * Whether current user has a capability or role for a given blog.
 *
 * @since 3.0.0
 *
 * @param int $blog_id Blog ID
 * @param string $capability Capability or role name.
 * @return bool
 */
function current_user_can_for_blog($blog_id, $capability)
{
    if (is_multisite()) {
        switch_to_blog($blog_id);
    }
    $current_user = wp_get_current_user();
    if (empty($current_user)) {
        return false;
    }
    $args = array_slice(func_get_args(), 2);
    $args = array_merge(array($capability), $args);
    $can = call_user_func_array(array($current_user, 'has_cap'), $args);
    if (is_multisite()) {
        restore_current_blog();
    }
    return $can;
}