WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Programatically changing user roles (4 posts)

  1. danhgilmore
    Member
    Posted 2 years ago #

    My predecessor had new users created and given a custom role, called "Blog Owner". In wp_usermeta, for the record 'wp_blogid_capabilities', the value is 'a:1:{s:5:"owner";b:1;}'. I want to change it to 'a:1:{s:13:"administrator";b:1;}'. I know I could do a MySQL UPDATE query, but I'd rather do it programatically within WordPress using their functions.

    I first query wp_usermeta for every record with the 'owner' value (9305 results returned), and I then pull the blog_id out of the meta_key (i.e., wp_23_capabilities). What would be the best way to update the users' role in the associated blog_id?

    Sorry if this doesn't explain it well. Let me know if you have questions.

  2. Helen Hou-Sandi
    WordPress Dev
    Posted 2 years ago #

    Does http://wordpress.stackexchange.com/questions/4725/how-to-change-a-users-role provide any new leads? Don't know how that would go over in multisite.

  3. danhgilmore
    Member
    Posted 2 years ago #

    Revisiting this again today.

    I have the following code:

    $results = $wpdb->get_results("select * from wp_usermeta where meta_value= 'a:1:{s:5:\"owner\";b:1;}';", ARRAY_A);
    if (is_array($results))
    {
    	foreach ($results as $result)
    	{
        $blogid = mb_substr($result['meta_key'], strpos($result['meta_key'], '_') +1, strrpos($result['meta_key'], '_') -3 );
        $user_id = $result['user_id'];
        echo 'Need to promote userid '. $userid . ' to Administrator on blogid  ' . $blogid .'<br>';
      }
    }

    So you see I have the userID and blogID. Is there an easy function like promote(userID, blogID)?

  4. danhgilmore
    Member
    Posted 2 years ago #

    Wow, I went and over-thought the problem. Here's the solution I came up with:

    switch_to_blog($blogid);
     $user = new WP_User($user_id);
     $user->remove_role('owner');
     $user->add_role('administrator');
     restore_current_blog();

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.