Forums

get user role from user id and blog id (2 posts)

  1. dgomez
    Member
    Posted 4 months ago #

    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

  2. dgomez
    Member
    Posted 3 months ago #

    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

Reply

You must log in to post.

About this Topic

Tags

No tags yet.