WP_Role::add_cap() WordPress Method

The WP_Role::add_cap() function is used to add a capability to a role. The first parameter is the name of the capability you wish to add. The second parameter is a boolean value that can be either true or false. If the value is set to true, then the capability will be given to the role. If the value is false, then the capability will be removed from the role.

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

Assign role a capability.


Parameters

$cap

(string)(Required)Capability name.

$grant

(bool)(Optional)Whether role has capability privilege.

Default value: true


Top ↑

More Information

Changing the capabilities of a role 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-role.php

	public function add_cap( $cap, $grant = true ) {
		$this->capabilities[ $cap ] = $grant;
		wp_roles()->add_cap( $this->name, $cap, $grant );
	}


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.