BuddyPress hack: remove non-friend updates from the activity stream
What else could you do with this simple my_is_friend_check function?
How about removing activity stream postings from non-friends all together (except public groups)
See our previous: #BuddyPress hack: remove new member registration from activity stream
function my_denied_activity_nonfriends( $a, $activities ) {
global $bp;
//if admin we want to know
if ( is_site_admin() )
return $activities;
foreach ( $activities->activities as $key => $activity ) {
/* if member of a group - we want the activity even if nonfriend */
if ( $activity->user_id != $bp->loggedin_user->id && $activity->component != 'groups' && $activity->component != 'blogs' && $activity->user_id != 0 && !my_is_friend_check($activity->user_id) && !my_is_follower_check($activity->user_id) && !my_is_atme_check($activity->content) ) {
unset( $activities->activities[$key] );
$activities->activity_count = $activities->activity_count-1;
$activities->total_activity_count = $activities->total_activity_count-1;
$activities->pag_num = $activities->pag_num -1;
}
}
/* Renumber the array keys to account for missing items */
$activities_new = array_values( $activities->activities );
$activities->activities = $activities_new;
return $activities;
}
add_action( 'bp_has_activities', 'my_denied_activity_nonfriends', 10, 2 );
Plus quick check functions – friend, nonfriend but made atme mention, follower ?
function my_is_atme_check( $content ) {
global $bp;
if ( !is_user_logged_in() )
return false;
if (!$content)
return false;
$pattern = '/[@]+([A-Za-z0-9-_]+)/';
preg_match_all( $pattern, $content, $usernames );
/* Make sure there's only one instance of each username */
if ( !$usernames = array_unique( $usernames[1] ) )
return false;
if ( in_array( bp_core_get_username( $bp->loggedin_user->id ), $usernames ) )
return true;
return false;
}
function my_is_friend_check( $friend_id = false) {
global $bp;
if ( !is_user_logged_in() )
return false;
if ( is_site_admin() )
return true;
if ( !$friend_id ) {
$potential_friend_id = $bp->displayed_user->id;
} else {
$potential_friend_id = $friend_id;
}
if ( $bp->loggedin_user->id == $potential_friend_id )
return false;
if ( friends_check_friendship_status($bp->loggedin_user->id, $potential_friend_id) == 'is_friend' )
return true;
return false;
}
function my_is_follower_check( $friend_id = false) {
global $bp;
if ( !is_user_logged_in() )
return false;
if ( is_site_admin() )
return true;
if ( !$friend_id ) {
$potential_friend_id = $bp->displayed_user->id;
} else {
$potential_friend_id = $friend_id;
}
if ( $bp->loggedin_user->id == $potential_friend_id )
return false;
if ( bp_follow_is_following( array( 'leader_id' => $potential_friend_id, 'follower_id' => $bp->loggedin_user->id ) ) )
return true;
}

March 11th, 2010 at 11:00 am
[...] the credit for this plugin goes to Rich Fuller and is entirely based on his tutorial for hacking BuddyPress to remove non-friend updates from the activity stream. I simply packaged it [...]
April 12th, 2010 at 6:55 am
Thanks for this,
It doesn’t show the user’s own update, only friend’s updates. Can you add a line in the conditional to show the user’s own updates please? That would be awesome!
June 1st, 2010 at 11:38 am
i replaced line 9 with the fix (also include blogs)
June 5th, 2010 at 3:21 pm
hi there. it still happens to me that it filters user’s own posts (and their friends comments con them), even though I uploaded the fix. Maybe I’m doing something wrong? ensalto.com/2com is the URL if you want to check it out. Thanks
June 7th, 2010 at 3:54 pm
make sure your not using the wpmu plugin but the code above. I added in the conditional fix so that is displays own updates. (also a followers check if someone is using that plugin)
But this is just an example, so feel free to change up those checks to something that suites your own needs. (line 10)
June 10th, 2010 at 5:45 pm
I’ve made the corrections and it works perfectly fine now. Thanks!! I had combined your corrected code with the plugin at WPMU but in a wrong way.
Keep up the good work, my friend!