Although it might seem a silly task, I simply can't find a method to get an array of all WordPress user roles that don't have a specific capability.

With the function bellow I'm able to get all available user roles, but how can I filter those so I can return only the user roles with or without a specific capability, let's say it's the upload_files capability? Would it be doable?

function get_roles_that_cant_upload_files() {
    global $wp_roles;

    if ( !isset( $wp_roles ) ) $wp_roles = new WP_Roles();

    $available_roles = array();

    $available_roles = $wp_roles->get_names();

    return $available_roles;

I've searched everywhere through WordPress docs and the web for a proper WP core function, which seems not to exist, not even a filter.

I was hoping to get only the user roles without the upload_files capability. Doing so, I'd use it to feed a select field within a plugin options and then I'd set another capability for the selected user roles.

I'm not a developer, so I've tried a few "hacks" without success. It seems the user capabilities are stored in wp_options table, this makes me think if would it be possible to perform a database query in order to get those results?

Any inputs appreciated.

1 Answer 1


Try with this:

function get_roles_that_cant($capability) {
    global $wp_roles;

    if ( !isset( $wp_roles ) ) $wp_roles = new WP_Roles();

    $available_roles_names = $wp_roles->get_names();//we get all roles names

    $available_roles_capable = array();
    foreach ($available_roles_names as $role_key => $role_name) { //we iterate all the names
        $role_object = get_role( $role_key );//we get the Role Object
        $array_of_capabilities = $role_object->capabilities;//we get the array of capabilities for this role
        if(!isset($array_of_capabilities[$capability]) || $array_of_capabilities[$capability] == 0){ //we check if the upload_files capability is present, and if its present check if its 0 (FALSE in Php)
            $available_roles_capable[$role_key] = $role_name; //we populate the array of capable roles
    return $available_roles_capable;

i took you function and add the logic to get the rol object and get all the capabilities for that object and check if the rol has that capability, also i made it general so you can send which capability you want to check, use it like this:


it will return an array like this:

    [contributor] => Contributor
    [subscriber] => Subscriber

so you can make your dropdown values using the $key of the array and the option string using the $value of the array.

  • Thank you so much for your help. It's a great function by the way! I've been trying to recreate the logic in order to do not return those user rules without a specific capability. If I use the function like get_roles_that_cant('upload_files'); I still get the user roles that "has" the upload_files cap, when in fact I'm looking for those that "has not" the upload_files capability. In my plugin options I intend to successfully prevent all user roles that already has the capability from being selected, this way those user roles would remain untouched. Commented Feb 15, 2017 at 18:07
  • i see it, i just updated the code, it was pretty late for me i made it the other way around by mistake, try it it should return the ones that dont have the capability now.
    – David Lee
    Commented Feb 15, 2017 at 18:14
  • Thanks @David Lee, you're a genius! My plugin is getting awesome thanks to you that helped me to overcome this issue. Have a look at it, I'm so glad t's working, cldup.com/b-gBoKnhyv.png Commented Feb 15, 2017 at 19:02
  • 1
    nice!, that is looking good by the way.
    – David Lee
    Commented Feb 15, 2017 at 19:33

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.