3

I have 500 users with 50 of those users assigned to user-role XYZ. I'd like these 50 XYZ users to keep their current XYZ user-role but also be assigned another/additional/extra/second user-role ABC... as a bulk job.

I'm looking for a function that could

  1. find all users with user-role XYZ
  2. then assign them to a second user-role of ABC
8
  • So you want a user to have the capabilities of both roles? Have you tried creating a new role that has those capabilities? A role is just a group of capabilities, you can create a new group with new capabilities – Tom J Nowell Dec 3 '16 at 13:43
  • No, the capabilities is not something I am concerned with. I only need an additional user-role assigned. – Pete Dec 3 '16 at 13:45
  • I'm not sure I understand, a role is a collection of capabilities with a name, a user can be in 1 of these 'groups', it's a single value stored in the database not an array or list. To allow the user to do something more you give them a capability. It sounds like you're repurposing the roles and capabilities system as a 'job role' not just security, you would be better off with a profile feed or a user taxonomy. Keep in mind that taking your question literally using WP terminology doesn't make sense – Tom J Nowell Dec 3 '16 at 14:00
  • 1
    @TomJNowell I'm not sure how well they're actually supported, but WP does indeed accept multiple roles per user - the {table_prefix}_capabiities usermeta entry is always a serialized array of role names. – TheDeadMedic Dec 3 '16 at 16:58
  • There are many plugins that can allocate more than one user role to a user. Whatever the capabilities are is not the issue, infact the capabilities may change. There are widget/menu visibility plugins that can be applied to different user roles. This is why I'm concerned with the user role not the capabilities. – Pete Dec 5 '16 at 5:30
3

You should be able to do this yourself by writing a simple function and hooking it it onto a user-related action (whether you want to do this once or on an ongoing basis will be up to you).

There are a couple different ways to trigger it, but perhaps the simplest I can think of would be to hook into the profile_update hook so that it would get run every time a user is saved. You could also hook into user_register so it runs everytime a new user is added to ensure your data stays in sync. In order to test this out, then, you would want to try saving an existing user or creating a new user, as this will trigger the action hook.

add_action( 'profile_update', 'wpse_assign_abc_role_to_xyz_users', 10 );
add_action( 'user_register', 'wpse_assign_abc_role_to_xyz_users', 10 );

function wpse_assign_abc_role_to_xyz_users() {
   $args = array(
        'role'         => 'xyz', // Set the role you want to search for here
        'role__not_in' => array( 'abc' ), // If they already have abc role, we can skip them
        'number'       => '500', // Good idea to set a limit to avoid timeouts/performance bottlenecks
    ); 
    $xyz_users = get_users( $args );

    // Bail early if there aren't any to update
    if ( count( $xyz_users ) === 0 ) return;

    // get_users() returns an array of WP_User objects, meaning we can use the add_role() method of the object

    foreach ( $xyz_users as $user ) {
        $user->add_role( 'abc' );
    }
}

This would assume that you have already added the abc role using add_role so that WP is aware of it.

Unfortunately, I am not available to test it right now but I will try and test later, however this should get you steered in the right direction.

3
  • Thanks Brian, I added this to my functions.php, I've also made a new user-role with a repo plugin. It didn't seem to work? – Pete Dec 7 '16 at 17:34
  • 1
    @Pete I just tried it on a fresh copy of WP 4.7 using the administrator and editor roles. The code works as written. Did you update the user roles from xyz and abc to the actual role slugs? Did you update/save a user to trigger the hook? You can try adding error_log( print_r( $xyz_users, true ) ); at the end of the function to see the output in the error log - there should be two entries under the role property. That is how I confirmed it worked. – brianjohnhanna Dec 7 '16 at 19:56
  • Bingo, it worked. I might have played around with it a little too much. Your original function worked. Cheers. This is a very useful function. I've looked everywhere and there's nothing around that adds an extra role in bulk, so hopefully this will help others. – Pete Dec 8 '16 at 2:37

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.