How can I display all Multisite blogs where this user is administrator?

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?

2 Answers
2

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 )

Leave a Comment