Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Given the user ID, how can I display all the blogs where this user is administrator?

I tried:

<?php
$user_id = 2;
$user_blogs = get_blogs_of_user( $user_id );
echo '<ul>';
foreach ($user_blogs AS $user_blog) {
    echo '<li>'.$user_blog->blogname.'</li>';
}
echo '</ul>';
?>

However, it will return all the blogs where user has access to regardless if it is an administrator or just a subscriber. But all I want to display is those blogs where this user is administrator.

Is this possible? If yes, How?

share|improve this question
add comment

2 Answers 2

up vote 4 down vote accepted

The capabilities are stored as user meta data in a serialized array for each site.

The key as a regular expression would look like that:

'~' . $GLOBALS['wpdb']->base_prefix . '(\d+)_capabilities~'

So … get the user meta data, find the capability strings (they hold the roles), and compare the unserialized result with the role you want to find.

Then get the blog ID, the (\d+) in the regex above, and you are done.

I think that is easier to understand with some code:

if ( ! function_exists( 'get_user_blogs_by_role' ) )
{
    /**
     * Get all blog IDs where the user has the given role.
     *
     * @param  int $user_id
     * @param  string $role
     * @return array
     */
    function get_user_blogs_by_role( $user_id, $role )
    {
        $out   = array ();
        $regex = '~' . $GLOBALS['wpdb']->base_prefix . '(\d+)_capabilities~';
        $meta  = get_user_meta( $user_id );

        if ( ! $meta )
            return array ();

        foreach ( $meta as $key => $value )
        {
            if ( preg_match( $regex, $key, $matches ) )
            {
                $roles = maybe_unserialize( $meta[$key][0] );

                // the number is a string
                if ( isset ( $roles[$role] ) and 1 === (int) $roles[$role] )
                    $out[] = $matches[1];
            }
        }

        return $out;
    }
}

Usage:

$blogs = get_user_blogs_by_role( 37, 'editor' );
var_dump( $blogs ); // Array ( 2, 14, 90 )
share|improve this answer
add comment

Here is an alternative way of doing it:

/**
 * Get the blogs of a user where they have a given role.
 *
 * @param int    $user_id The ID of the user.
 * @param string $role    The slug of the role.
 *
 * @return object[] The blog details for each blog the user has the role for.
 */
function get_blogs_of_user_by_role( $user_id, $role ) {

    $blogs = get_blogs_of_user( $user_id );

    foreach ( $blogs as $blog_id => $blog ) {

        // Get the user object for the user for this blog.
        $user = new WP_User( $user_id, '', $blog_id );

        // Remove this blog from the list if the user doesn't have the role for it.
        if ( ! in_array( $role, $user->roles ) ) {
            unset( $blogs[ $blog_id ] );
        }
    }

    return $blogs;
}

The use is similar to @toscho's function, but the output in this case is of the same format as returned by get_blogs_of_user().

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.