WP_User::add_cap()publicWP 2.0.0

Add capability and grant or deny access to capability.

Method of the class: WP_User{}

No Hooks.

Return

null. Nothing (null).

Usage

$WP_User = new WP_User();
$WP_User->add_cap( $cap, $grant );
$cap(string) (required)
Capability name.
$grant(true|false)
Whether to grant capability to user.
Default: true

Examples

0

#1 Add a new feature to an individual user

Let's allow the user 20 to edit posts.

$user = new WP_User( 20 );
$user->add_cap( 'can_edit_posts' );

Changelog

Since 2.0.0 Introduced.

WP_User::add_cap() code WP 6.5.2

public function add_cap( $cap, $grant = true ) {
	$this->caps[ $cap ] = $grant;
	update_user_meta( $this->ID, $this->cap_key, $this->caps );
	$this->get_role_caps();
	$this->update_user_level_from_caps();
}