BuddyPress Fun: Playing with BuddyPress Friends List Visibility
Are you ready for another BuddyPress trick ? Well, today, we will be playing with the visibility of friends list on BuddyPress based sites. I got some time yesterday to play with the friend list visibility and I am sure some of you will find it helpful.
The goal of this tutorial is to control who can see a user's friend list on BuddyPress based site.
I will be covering 3 appraoches ( from the most extreme to easy and flexible ).
Approach 1:- Only a user can view His/Her friends list
Now, this is the most extreme way to implement it I guess. All we do here is just deregister the nav for friends component as shown below.
1 2 3 4 5 6 7 8 9 | add_action( 'bp_friends_setup_nav', 'devb_custom_hide_friends_if_not_self' ); function devb_custom_hide_friends_if_not_self() { if ( bp_is_my_profile() || is_super_admin() ) return; bp_core_remove_nav_item( 'friends' ); } |
When the logged in user is super admin or He/she is viewing their own profile, we don't remove the nav, otherwise we remove the nav.
Though it works, For me, It seems the extremes. So, Let us forget it and move to our next spproach.
Approach 2: Filter on bp_ajax_querystring to hide the friends from listing
In the code below, we will only show the friends list if it is user's own profile or the user is logged in as site admin.
1 2 3 4 5 6 7 8 9 10 11 | add_filter( 'bp_after_has_members_parse_args', function ( $args ) { if ( is_super_admin() ) { return $args; } if ( ! empty( $args['user_id'] ) && $args['user_id'] != get_current_user_id() ) { $args['user_id'] = array( 0, 0 ); //invalid } return $args; } ); |
Now, the code above hides the friends list from everyone except the user and the super admins.
So, how about adding a little more flexibility and allowing the friends of a user to view his/her friend list? Ok, That's not difficult, all we need to do is change the above code and add a check for friendship as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 | add_filter( 'bp_after_has_members_parse_args', function ( $args ) { if ( is_super_admin() ) { return $args; } if ( ! empty( $args['user_id'] ) && $args['user_id'] != get_current_user_id() && function_exists( 'friends_check_friendship' ) && ! friends_check_friendship( $args['user_id'], get_current_user_id() ) ) { $args['user_id'] = array( 0, 0 ); //invalid } return $args; } ); |
The above code allow users, site admins and friends of the user to view their friend list but non friends and non logged in users won't be able to see the friend list.
Please note, you will need only one of the above three code snippets. Put it in your bp-custom.php and you may want to wait for my next tip tomorrow 🙂
How to hide myself from members lists?
In Members and in Single Group>members, also in Single Member>friends..
Thanks!
Hi Mihail,
Please look at our BuddyPress profile visibility plugin. That allows you to hide from everywhere.
Hi Brajesh,
I know about that plugin, however I don't want to hide myself from members listing, but hide myself from ME.
Seeing myself on lists as member or somebodies friend makes no sense to me…
Thank you.
Hi Mihail,
do you mean that when you are logged in It should not show you in any of the list? That sound interesting. Can you please confirm if I am right about that?
Correct, I see myself all the time… members, group members, friends list of my friends (but this one I hided completely as you described in this tip… btw, big THANKS for that…
You are most welcome. Just one more question, should It happen for all users or you want it only for the admins?
I haven't seen any social network where u can see yourself listed anywhere, except comments, likes etc… and it says "You", not your name 🙂
It pretty disturbing if you pay attention to it… when u're a group admin, then you see yourself twice "once as a group admin in group header, and once in members"… makes no sense really…
Thank you.
all users… nobody should see him/her self on any list…
Simply "exclude" current user from lists…
for example https://buddydev.com/members/
https://buddydev.com/members/XYXYX/friends/
etc…
I don't want to be on those list…
Posting code as a new post in 30 mins. You gave me the idea for today's post. Thank you Mihail 🙂
U're more than welcome Brajesh.
Thank you!
Here we go 🙂
https://buddydev.com/buddypress-tricks/hide-logged-users-listings-directory-search-members-friends-list-widgets-etc-buddypress-site/
Can you modify Approach 2 to protect any other tabs content create by other plugins. I like this approach because still left navigation intact but show "Sorry, no members were found."
Maybe could change "Sorry, no content were found." if that tab access from not own profile, change from what I do now that redirect them to homepage