• If I know the user id and blog id how can I get a users role in that blog?

    I’m trying to create a list of blogs for a user with the user’s role in the blog listed with the blog name

Viewing 1 replies (of 1 total)
  • Thread Starter dgomez

    (@dgomez)

    As a followup here is what I got to work:

    <?php
    $user_sites = get_blogs_of_user($user_id);
    
    if ($user_sites) {
        echo '<p><strong>Current list of sites for this user:</strong></p>';
    
        foreach ($user_sites as $user_site) {
    
            echo $user_site->blogname;
            switch_to_blog($user_site->userblog_id);
            $theuser = new WP_User($user_id, $user_site->userblog_id);
            if (!empty($theuser->roles) && is_array($theuser->roles)) {
                foreach ($theuser->roles as $role) {
                    echo ' - ' . $role;
                }
            }
            restore_current_blog();
        }
    }
    ?>

    I don’t know why it is necessary to use switch_to_blog when the blog id is used with WP_User but it only returns the main blog role in a multisite blog otherwise

Viewing 1 replies (of 1 total)
  • The topic ‘get user role from user id and blog id’ is closed to new replies.