WP_User::add_cap() WordPress Method

The WP_User::add_cap() function allows a user to be given a new capability. This function can be useful when a user needs to be given access to a new feature or area of the site.

WP_User::add_cap( string $cap, bool $grant = true ) #

Adds capability and grant or deny access to capability.


Parameters

$cap

(string)(Required)Capability name.

$grant

(bool)(Optional)Whether to grant capability to user.

Default value: true


Top ↑

More Information

Changing the capabilities of a user is persistent, meaning the added capability will stay in effect until explicitly revoked.

This setting is saved to the database (in table wp_options, field wp_user_roles), so it might be better to run this on theme/plugin activation.


Top ↑

Source

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

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


Top ↑

Changelog

Changelog
VersionDescription
2.0.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.