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?
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:
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:
Usage:
Here is an alternative way of doing it:
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()
.