Skip to:
Content
Pages
Categories
Search
Top
Bottom

Re: Hiding “subscriber” role blogs from My Blogs on admin bar


Burt Adsit
Participant

@burtadsit

Here’s two functions that change the way the admin bar displays user blogs.

// sort array of blog objs by $blog->role according to the order of $blog_roles
// roles not included in $blog_roles array will not be displayed
function filter_blogs_by_role($blogs){
global $bp, $blog_roles;

$blog_roles[] = __( 'Admin', 'buddypress' );
$blog_roles[] = __( 'Editor', 'buddypress' );
$blog_roles[] = __( 'Author', 'buddypress' );
$blog_roles[] = __( 'Contributor', 'buddypress' );
$blog_roles[] = __( 'Subscriber', 'buddypress' );

// get roles
foreach ($blogs as $blog){
$blog->role = get_blog_role_for_user( $bp->loggedin_user->id, $blog->userblog_id );
}

// eliminate roles not in $blog_roles
foreach ($blogs as $key => $value){
if (!in_array($value->role, $blog_roles))
unset($blogs[$key]);
}

// sort by $blog_roles sequence if there are any left
if ($blogs){
usort($blogs, 'compare_roles');
}

return $blogs;
}

// i love php.net. stolen from php.net usort manual, user mkr at binarywerks dot dk's
// contribution for priority list comparision function.
function compare_roles($a, $b){
global $blog_roles;

foreach($blog_roles as $key => $value){
if($a->role==$value){
return 0;
break;
}

if($b->role==$value){
return 1;
break;
}
}
}

You can put those two fns in your bp-custom.php file. The fn filter_blogs_by_role() does the work and the fn compare_roles() is just a helper fn.

filter_blogs_by_role() takes the array of blog objects returned by the fn get_blogs_of_user() in the admin bar’s bp_adminbar_blogs_menu() fn. It first gets all the roles for the user’s blogs and then removes all of the roles that are not listed in the $blog_roles array. That way you can eliminate some roles such as ‘Subscriber’ if you want. Next it sorts the blogs by the sequence of roles defined in $blog_roles. However you list them in that array is how they will display in the admin menu.

1) So, put those two fns in bp-custom.php

2) Arrange the roles defined by $blog_roles in the fn filter_blogs_by_role() however you want the roles to appear in the menu

3) Comment out the roles you do *not* want to appear in the menu

4) Put the call to filter_blogs_by_role() on line 133 in bp-core-adminbar.php like this:

if ( !$blogs = wp_cache_get( 'bp_blogs_of_user_' . $bp->loggedin_user->id, 'bp' ) ) {
$blogs = get_blogs_of_user( $bp->loggedin_user->id );
wp_cache_set( 'bp_blogs_of_user_' . $bp->loggedin_user->id, $blogs, 'bp' );
}

$blogs = filter_blogs_by_role($blogs);

Just in case the forums trash the code I also stuck it here: http://pastie.org/445729

Skip to toolbar