WP_Roles::add_cap()
Adds or removes a capability for the specified role.
Changes to role or user capabilities are written to the database. Therefore this function should be called once, on plugin or theme activation/deactivation.
This is a class method: WP_Roles.
Use WP_User::add_cap() to add/remove a capability for an individual user.
Method of the class: WP_Roles{}
No Hooks.
Returns
null. Does not return anything.
Usage
// для WP $roles = new WP_Roles( $user_id ); $roles->add_cap( $role, $cap, $grant );
- $role(string) (required)
- Role name: Super Admin, Administrator, Editor, Author, Contributor, Subscriber.
- $cap(string) (required)
- Capability name. Table of roles and their default capabilities.
- $grant(boolean)
- Whether the specified role can use the specified capability. I.e. allow this capability (true) or deny it (false).
Default: true
Examples
#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() WP Roles::add cap code WP 7.0
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 );
}
}