WP_User::has_cap( string $cap )

Whether user has capability or role name.

Description Description

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

Parameters Parameters


(string) (Required) Capability name.


(int) (Optional) ID of the specific object to check against if $cap is a "meta" cap. "Meta" capabilities, e.g. 'edit_post', 'edit_user', etc., are capabilities used by map_meta_cap() to map to other "primitive" capabilities, e.g. 'edit_posts', 'edit_others_posts', etc. The parameter is accessed via func_get_args() and passed to map_meta_cap().

Top ↑

Return Return

(bool) Whether the current user has the given capability. If $cap is a meta cap and $object_id is passed, whether the current user has the given meta capability for the given object.

Top ↑

Source Source

File: wp-includes/class-wp-user.php

	public function has_cap( $cap ) {
		if ( is_numeric( $cap ) ) {
			_deprecated_argument( __FUNCTION__, '2.0.0', __('Usage of user levels by plugins and themes is deprecated. Use roles and capabilities instead.') );
			$cap = $this->translate_level_to_cap( $cap );

		$args = array_slice( func_get_args(), 1 );
		$args = array_merge( array( $cap, $this->ID ), $args );
		$caps = call_user_func_array( 'map_meta_cap', $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) )
				return false;
			return true;

		 * Dynamically filter a user's capabilities.
		 * @since 2.0.0
		 * @since 3.7.0 Added the user object.
		 * @param array   $allcaps An array of all the user's capabilities.
		 * @param array   $caps    Actual capabilities for meta capability.
		 * @param array   $args    Optional parameters passed to has_cap(), 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;

		// Must have ALL requested caps.
		foreach ( (array) $caps as $cap ) {
			if ( empty( $capabilities[ $cap ] ) )
				return false;

		return true;

Top ↑

Changelog Changelog

Version Description
2.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.