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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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 )
Method 2
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().
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0