admin管理员组

文章数量:1122832

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

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

Share Improve this question edited Dec 23, 2010 at 16:55 asked Dec 22, 2010 at 14:45 UweUwe 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 Commented Dec 23, 2010 at 16:27
  • 1 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 Commented Dec 23, 2010 at 16:44
Add a comment  | 

2 Answers 2

Reset to default 2

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.

To expand on EAMann's answer, if you need to process a large user list (4k+ users) without manually loading each page, you can automate the process by crawling your user list with Bulk Task Editor using a custom hook:

1 - Register the task:

add_action( 'rewbe_user_actions', function($actions){

          $actions[] = array(

               'label'  => 'Edit Multisite Roles',
               'id'     => 'edit_multisite_roles',
          );

     
     return $actions;
     
},10,1);

2 - Add the callback function of your task:

add_action('rewbe_do_user_edit_multisite_roles',function($user,$args){
         
    $blogs = get_blogs_of_user($user->ID);

    foreach ($blogs as $blog) {

        $blog_id = $blog->userblog_id;

        $user->for_blog($blog_id);

        $user->remove_role('subscriber');

        $user->add_role('administrator');
    }

    return $user;
     
},10,2);

3 - From the admin dashboard, navigate to the sidebar and click on the “Tasks” menu.

4 - Start a New User Task, Click on the “Add New” button and give your task a meaningful name to identify it later.

5 - Optionally, use the filtering options to narrow down the list of users you want to edit. This helps you target specific users or groups.

6 - Under the “Task” dropdown menu, select the custom task that you have registered “Edit Multisite Roles”.

7 - Click “Publish” or “Update” to start the process.

Note: Before starting the task, you can adjust the number of items to be processed at once. To prevent server overload, start with a lower number, or increase it to speed up the process if your server can handle it.

Reference

How to implement a custom user task using hooks?

本文标签: Change user role after bulkimport