I want to create dynamic capabilities using WP_Role::add_cap(), something like

$author_role = get_role('author');

foreach ($sections as $section) {
    $author_role->add_cap( "edit_{$section}_pages", /* grant= */ false );
}

and then let administrators grant certain of those capabilities to certain authors only (e.g., Jane can edit_school_pages) in wp-admin (not programmatically, via WP_User::add_cap()).

I assume a plugin can help with this, but those I’ve found (e.g., Capability Manager) seem to emphasize creating new Roles or Capabilities and adding/moving Capabilities among Roles, not granting capabilities directly to users (though I could be wrong).

Advice? Thanks, s

3 s
3

you can assign capabilities directly to user using the class WP_user::add_cap()

//to remove capability from user
$user = new WP_User( $user_id );
$user->remove_cap( 'can_email');

//to add capability to user
$user = new WP_User( $user_id );
$user->add_cap( 'can_email');

Source – https://wordpress.stackexchange.com/a/60433/17968

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *