5

How can I get ALL the capabilities of an existing user role (eg. Author) ? I'll be using it to quickly duplicate an existing user role, then create a new role then assign or remove capabilities to a new custom role.

1
  • 1
    Something like get_role( $role )->capabilities e.g get_role( 'author' )->capabilities ? ..
    – Ismail
    May 21, 2016 at 16:50

1 Answer 1

9

I suggest not to mess with the existing roles and capabilities in its default, it might help you some trouble when you will have a bunch of custom roles and capabilities in the future.

To answer your question, I will have an example: I need new role/user group that has almost an Administrator but don't have the the capability to edit plugins and the theme, to do that:

  1. Let's say you would like to copy the existing capabilities of Administrator, do it by:

    $admin_role_set = get_role( 'administrator' )->capabilities;
    
  2. You can create a new user role by using add_role, see the handbook for more details. We can then use the list of capabilities that we have from #1 by using:

    $role = 'content_admin';
    $display_name = 'Content Admin';
    add_role( $role, $display_name, $admin_role_set );
    
  3. By checking the Roles and Capabilities list from the handbook, we will be needing to remove edit_plugins & edit_themes capabilities, we can achieve that by using remove_cap():

    remove_cap( 'content_admin', 'edit_plugins' );
    remove_cap( 'content_admin', 'edit_themes' );
    
2
  • 1
    In the 3rd step that you outline, shouldn't the role supplied to the remove_cap method be 'content_admin' instead of 'editor' if you're continuing the example of removing the the capability to edit plugins and the theme for the newly created custom role?
    – Kory
    May 7, 2020 at 18:21
  • Edited as per @Kory comment Aug 19, 2021 at 11:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.