Support » Networking WordPress » Programatically changing user roles

  • Resolved danhgilmore


    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.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Does provide any new leads? Don’t know how that would go over in multisite.

    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)?

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

     $user = new WP_User($user_id);
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Programatically changing user roles’ is closed to new replies.