1

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

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'.

2
  • As I mentioned below, make sure you use $user->remove_role('subscriber') otherwise they'll be both an admin and a subscriber ... which isn't the functionality you want.
    – EAMann
    Dec 23, 2010 at 16:27
  • OK, if the user is an "Editor", then use $user->remove_role('editor'); ... you need to remove their current role so that their only role is administrator. And yes, I'm sure this is the right function - see the inline documentation of the function in the WP_User class.
    – EAMann
    Dec 23, 2010 at 16:44

1 Answer 1

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.

10
  • Thank you but this solves not my problem. Yes it's a multisite installation. Every user has one own blog, every blog has one user. But after import, he can only write and edit his articles, not administrate the blog settings. After import, new registered users have the role administrator, all the imported users are only subscribers for their blog. Maybe there is a WP function like set_user_role_for_blog ($user_id; $blog_id; $role) (example for understanding) I can run once. AFAIK is this information in usermeta? Has anyone a code for direct DB change?
    – Uwe
    Dec 22, 2010 at 17:46
  • Please see my update for an example with code ...
    – EAMann
    Dec 22, 2010 at 18:43
  • What's wrong here? [code]include 'wp-load.php'; global $wpdb; for ( $blog_id = 393; $blog_id <=400; $blog_id++ ){ //limited for testing $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.'<br />'; //control check if (!$user_id == '') { $user = new WP_User($user_id); $user->for_blog($blog_id); $user->add_role('administrator'); } } } echo 'Done!'; [/code] There is no change to user's role.
    – Uwe
    Dec 23, 2010 at 16:00
  • @Uwe your code is unreadable in a comment. Please amend your original question so we can read what you're doing.
    – EAMann
    Dec 23, 2010 at 16:03
  • Very quick response thx. Post is edited.
    – Uwe
    Dec 23, 2010 at 16:09

Your Answer

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