WP_Roles::add_cap()publicWP 2.0.0

Add capability to role.

Method of the class: WP_Roles{}

No Hooks.

Return

null. Nothing (null).

Usage

global $wp_roles;
$wp_roles->add_cap( $role, $cap, $grant );
$role(string) (required)
Role name.
$cap(string) (required)
Capability name.
$grant(true|false)
Whether role is capable of performing capability.
Default: true

Examples

1

#1 Add an option for all users with the "author" role

Allow authors to edit other posts (besides their own):

register_activation_hook( __FILE__, 'add_theme_caps' );

function add_theme_caps() {

	// get the author role. At the same time connect to the WP_Role class
	$role = get_role( 'author' );

	 // add a new feature
	$role->add_cap( 'edit_others_posts' ); 
}

Changelog

Since 2.0.0 Introduced.

WP_Roles::add_cap() code WP 6.5.2

public function add_cap( $role, $cap, $grant = true ) {
	if ( ! isset( $this->roles[ $role ] ) ) {
		return;
	}

	$this->roles[ $role ]['capabilities'][ $cap ] = $grant;
	if ( $this->use_db ) {
		update_option( $this->role_key, $this->roles );
	}
}