0

In my multi-site network, I am using "User Role Editor" plugin to assign capabilities to roles. I want the site-admins (administrators) of sub-sites to have capability to change role of any user to any other role except the super-admin role. The only way to achieve that, as per my limited understanding, is to hide the super-admin role from the options of "change role to" menu on the user listing page.

I can hide the administrator and the editor roles for the aforementioned menu, using the following code available here:

function wdm_user_role_dropdown($all_roles) {

    global $pagenow;

    if( current_user_can('editor') && $pagenow == 'user-edit.php' ) {
        // if current user is editor AND current page is edit user page
        unset($all_roles['administrator']);
        unset($all_roles['editor']);
    }

    return $all_roles;
}
add_action ('editable_roles','wdm_user_role_dropdown');

Based on this, I tried following code to hide the super-admin role as desired:

function schoolers_user_role_dropdown($all_roles) {

    global $pagenow;

    if( current_user_can('remove_users') && $pagenow == 'user-edit.php' ) {
        // if current user can "remove_users" AND current page is edit user page
        unset($all_roles['administrator']);
        unset($all_roles['superadmin']);
    }

    return $all_roles;
}
add_action('editable_roles','schoolers_user_role_dropdown');  

This modified code, however, is not hiding super-admin.

While editing the reference code, I had assumed that slug for Super-admin role is either superadmin or super_admin. But when both didn't work, I searched for the required slug and stumbled upon the fact that Super-admin role does not have a slug. So, possibly, we can not hide super-admins like that.

Can any one suggest a correction in the above code and/or help with another method to achieve what I need?

Thanks!

  • what's print_r( $all_roles ); ? Not sure why you have superadmin as an option in the select box. – birgire Jul 22 '17 at 9:28
2

As it turned out this is not a general problem. This was due to "User Role Editor" plugin itself so people not using subject plugin may not face such an issue.

Anyways, author of "User Role Editor" plugin has now provided a solution to circumvent this default behavior here. So, one has to use following code snippet:

    add_filter ('ure_not_block_other_roles_for_local_admin','my_not_block_other_roles_for_local_admin');

function my_not_block_other_roles_for_local_admin($flag) {
         return false;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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