WP_User::add_cap()publicWP 2.0.0

Adds or removes the right (ability) for the specified user.

The change of right is recorded in the database. Therefore, this function should be called only once, during the activation/deactivation of the plugin or theme.

This is a class method: WP_User{}

Use WP_Roles::add_cap() to add/remove the right entirely for a WordPress role.

Method of the class: WP_User{}

No Hooks.

Returns

null. Nothing.

Usage

$WP_User = new WP_User();
$WP_User->add_cap( $cap, $grant );
$cap(string) (required)
The name of the capability. The default capabilities table see here.
$grant(true/false)
Allow this capability (true) or conversely deny it (false).
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 7.0

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();
}