BuddyPress Friends Only User Profile Privacy
Making your BuddyPress user profiles friends only is easy. I have seen a lot of people asking about it and today, we will see how to do it.
Our goal is to restrict access to user profiles to everyone except:-
- Self:- The user themselves should be allowed to access their profile.
- Site Admins:- There should be no restriction in site admins
- Friends:- Users who are friends with the user being visited
- Friendship requested:- Users who have been requested friendship by the profile owner.
You can add the following code to your to theme's functions.php or bp-custom.php in your wp-content/plugins directory
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | /** * Restrict profile access to friends, super admins and users who have been requested friendship by the displayed user. */ function buddydev_friends_only_profile() { // Not a profile page or friends component is not active, don't do anything. if ( ! bp_is_user() || ! bp_is_active('friends' ) ) { return; } $where_to_redirect = site_url( '/' ); // special case, if a user is not logged in, we do not allow access. if ( ! is_user_logged_in() ) { bp_core_add_message( __( 'Sorry, The profile is protected.' ), 'error' ); // bp_core_redirect( $where_to_redirect ); } // if we are here, a user is logged in and it must be profile. // if we are on user's own profile or it is super admin // or friend or friendship requested by profile owner to logged user // do not restrict. if ( bp_is_my_profile() || is_super_admin() ) { return; } $user_id = bp_loggedin_user_id(); $displayed_user_id = bp_displayed_user_id(); if ( friends_check_friendship( $user_id, $displayed_user_id ) || 'awaiting_response' == friends_check_friendship_status( $user_id, $displayed_user_id ) ) { return; } bp_core_add_message( __( 'Sorry, The profile is protected.' ), 'error' ); // bp_core_redirect( site_url( '/' ) ); } add_action( 'bp_template_redirect', 'buddydev_friends_only_profile' ); |
You can changeĀ $where_to_redirect to point to anywhere you want the users to be redirected.
Have fun!
What would be really great would be to make it so that each member of your community could choose to have this for their profile but an overall community setting of site members able to view all profiles.
Possible to make a plugin to do that?
Dale.
Thank you for the suggestion Dale.
You can do it using our BuddyPress Profile Visibility Manager
https://buddydev.com/plugins/bp-profile-visibility-manager/
Regards
Brajesh