When you add user roles, you can add capabilities at the same time. But, sometimes you want to add or remove capabilities from a user role that already exists.
How to add capabilities?
To add capabilities to a role, you need to get the role first using get_role() and then add capabilities using add_cap() method.
function guest_editor_caps() {
// Gets the simple_role role object.
$role = get_role( 'guest_editor' );
// Add a new capability.
$role->add_cap( 'edit_others_posts', true );
}
// Add simple_role capabilities
// priority must be after the initial role definition.
add_action( 'init', 'guest_editor_caps', 11 );
How to remove capabilities?
All you need to do is change add_cap() method to remove_cap() method in the code above. That’s it.
function guest_editor_caps() {
// Gets the simple_role role object.
$role = get_role( 'guest_editor' );
// Add a new capability.
$role-> remove_cap( 'edit_others_posts', true );
}
// Add simple_role capabilities
// priority must be after the initial role definition.
add_action( 'init', 'guest_editor_caps', 11 );
capabilities functions hook role user