Change user role after bulk-import

after successful import of ~4000 users and their blogs & posts from Liftype I have a rights problem: all users only are subscribers in their own blog. How can I mass change it to blog (not sitewide!) administrator privileges? All known functions I found in Codex gives Admin rights for the main blog, that’s not what I want.

Thanks
Uwe

Read More

Edit:
What’s wrong here?

include 'wp-load.php';
global $wpdb;
for ( $blog_id = 393; $blog_id <=4091; $blog_id++ ){

  $bloguser= get_users_of_blog ($blog_id);
    foreach ($bloguser as $usr) { // maybe there is more than one
        $user_id = $usr->ID;
          echo 'Blog No. '.$blog_id.' is property of user: '.$user_id;  //control check
      if ($user_id != '') {
        $user = new WP_User($user_id);
        $user->for_blog($blog_id);
        $user->remove_role('editor');
        $user->add_role('administrator');
      }
    }
}
echo 'Done!';

Code is well running, but there is no change to user’s role.

Edit: code typo edited, no changes to result

@EAMann Are you sure, add_role is the right function? I understand this is for adding a new role to WP system.

Edit: Updated the code again. Using update_user_meta. $check gives back ‘administrator’ (right!), but if I call the user’s blog properties, the user is always ‘editor’.

Related posts

Leave a Reply

1 comment

  1. From your question, I’m assuming these are users on a specific blog within a MultiSite installation, correct? In that case, do the following:

    1. Go to the blog for which you want to make these users administrators.
    2. Click on the “Users” button in the left-side admin sidebar
      • This will present you with a display of about 20 users at a time (the display is paged)
    3. Click the checkbox next to the users you wish to promote
    4. At the top of the list is a dropdown box that says “Change role to…” – Select “Administrator” from this select box
    5. Click the “Change” button to the right of the select box

    You’ll have to do this for each page of users, and with 4000 users you’ll have about 160 pages of results. But it’s doable.


    Update

    If you want some specific code, I recommend looking at the WP_User class. This class defines two methods that you’d need to use iteratively: for_blog() and add_role().

    Basically, you’ll need to loop through your users based either on their IDs or usernames. Consider this untested example code:

    $ids = [1,2,3,4];
    foreach($ids as $id) {
        $user = new WP_User($id);
        $user->for_blog( ... user's blog id ... );
        $user->remove_role('subscriber');
        $user->add_role('administrator');
    }
    

    By default, the add_role() method of the WP_User class will act on the current blog … you use for_blog() to switch to a specific blog before running the add_role() method.

    So if you have the ids of your users and the ids of the blogs they’re supposed to be administrators for, you can loop through them pretty easily and set them up as administrators for their specific sites.