user_has_cap filter-hookWP 2.0.0

Dynamically filter a user's capabilities.

Usage

add_filter( 'user_has_cap', 'wp_kama_user_has_cap_filter', 10, 4 );

/**
 * Function for `user_has_cap` filter-hook.
 * 
 * @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.
 * @param WP_User  $user    The user object.
 *
 * @return bool[]
 */
function wp_kama_user_has_cap_filter( $allcaps, $caps, $args, $user ){

	// filter...
	return $allcaps;
}
$allcaps(bool[])
Array of key/value pairs where keys represent a capability name and boolean values represent whether the user has that capability.
$caps(string[])
Required primitive capabilities for the requested capability.
$args(array)

Arguments that accompany the requested capability check.

  • 0(string)
    Requested capability.

  • 1(int)
    Concerned user ID.

  • ...$2(mixed)
    Optional second and further parameters, typically object ID.
$user(WP_User)
The user object.

Changelog

Since 2.0.0 Introduced.
Since 3.7.0 Added the $user parameter.

Where the hook is called

WP_User::has_cap()
user_has_cap
wp-includes/class-wp-user.php 813
$capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args, $this );

Where the hook is used in WordPress

wp-includes/default-filters.php 713
add_filter( 'user_has_cap', 'wp_maybe_grant_install_languages_cap', 1 );
wp-includes/default-filters.php 714
add_filter( 'user_has_cap', 'wp_maybe_grant_resume_extensions_caps', 1 );
wp-includes/default-filters.php 715
add_filter( 'user_has_cap', 'wp_maybe_grant_site_health_caps', 1, 4 );