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;
}
This entry was posted in Tech and tagged , . Bookmark the permalink.

8 Responses to BuddyPress hack: remove non-friend updates from the activity stream

  1. Pingback: Create a Better Community Experience With the New BuddyPress Friends-Only Activity Stream Plugin - WordPress MU and BuddyPress plugins, themes, support, tips and how to's

  2. Celso Soares says:

    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!

  3. admin says:

    i replaced line 9 with the fix (also include blogs)

  4. fede3456 says:

    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

  5. admin says:

    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)

  6. fede3456 says:

    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!

  7. shayne says:

    I have the Friends only Activity Streamplugin installed. But how did you guys manage to make it not filter out my own activities? Help would be much appreciated. I have tried adding the line “$activity->user_id != $bp->loggedin_user->id” to the plugin, but it doesnt allow my activity to be shown.

  8. kanowah says:

    hey good news just find this line
    if ( $bp->loggedin_user->id == $potential_friend_id )
    return false;

    change it to

    if ( $bp->loggedin_user->id == $potential_friend_id )
    return true;

    this will help you to show friend and your own activity

Leave a Reply