If you are using BuddyPress Profile visibiliy manager plugin, the plugin allows users to set their profile to various privacy levels.
If a user does not meet the visibility requirement and tries to visit other user’s profile, he gets redirected to the referrer page(last page from where he clicked the link). If the referrer page is empty, he will be redirected to the home page of the site. Additionally, a notice message is added via BuddyPress bp_core_add_message to notify the users of the privacy limitation.
In case you want to redirect the user to a specific page when they do not meet the visibility requirement, you can filter on ‘bp_profile_visibility_redirect_location‘.
Usage:-
1 | add_filter( 'bp_profile_visibility_redirect_location', 'your_function_name' , 10, 2 ); |
where “your_function_name” is the name of the function to be called. The function is passed 2 arguments in the following order:-
1 2 3 4 5 6 7 | function your_function_name( $location, $privacy ){ //return your custom location return $location;//modify and return your own location } |
- $location:- the location where the visitor will be redirected if you do not modify it
- $privacy:- one of the privacy levels,. Possible values are (‘public’, ‘friends’,’self’)
Example 1:- Redirect to a page on site if a user can not access the profile.
1 2 3 4 5 6 7 8 | function buddydev_visibility_global_redirect( $location, $privacy ) { $location = "http://example.com/some-global-page";//please change it return $location; } add_filter( 'bp_profile_visibility_redirect_location', 'buddydev_visibility_global_redirect', 10, 2 ); |
Example 2:- Based on privacy level, Redirect to different page when the profile access is denied
1 2 3 4 5 6 7 8 9 10 11 12 13 | // Conditionally redirect to page based on privacy. function buddydev_visibility_conditionally_redirect( $location, $privacy ) { if ( $privacy == 'loggedin' ) { $location = "http://example.com/explanation-page-that-user-must-be-loggedin/"; } elseif ( $privacy == 'friends' ) { $location = "http://example.com/page-explaining-that-only-friends-can-see-this-user"; } return $location; } add_filter( 'bp_profile_visibility_redirect_location', 'buddydev_visibility_conditionally_redirect', 10, 2 ); |
You can put these code in your bp-custom.php or in your theme’s functions.php