bump I want to also block access to subscriber profiles. The admin code above blocks the admin perfectly but how do you change it to block all subscribers.
I found this code but it does not work to block subscribers. No errors just doesn’t work
function redirect_members() {
// redirect non logged-in members
if ( !is_user_logged_in() ) {
// get the ID of displayed member
$displayed_user = bp_displayed_user_id();
if (in_array($displayed_user,get_subscriber())) {
wp_redirect(site_url(‘/your-new-page-slug’));
exit();
}
}
}
add_action( ‘wp’, ‘redirect_members’, 1 );
// get all subscribers
function get_subscribers() {
$blogusers = get_users( ‘role=subscriber’ );
// Array of WP_User objects.
$exclude = array();
foreach ( $blogusers as $user ) {
$exclude[] .= esc_html( $user->ID );
}
return $exclude;
}
Here’s some untested code that should prevent anyone from visiting the profile of a subscriber user. Roles are not a BP construct, so you’ve got to look to WordPress for that info.
add_action( 'wp', function() {
if ( bp_is_profile() ) {
$user_meta = get_userdata( bp_displayed_user_id() );
if ( in_array( 'subscriber', $user_meta->roles; ) ) {
wp_redirect( home_url() );
exit;
}
}
}, 1 );
I added your code to my functions.php page and all I got was a white screen?
Are you using part of my code from above or is this separate code where do I add this? I don’t understand what your saying about Roles?
really appreciate you helping on this thanks again
Ha, that’s because there was an extra semi-colon. Try this:
add_action( 'wp', function() {
if ( bp_is_profile() ) {
$user_meta = get_userdata( bp_displayed_user_id() );
if ( in_array( 'subscriber', $user_meta->roles ) ) {
wp_redirect( home_url() );
exit;
}
}
}, 1 );
What I was saying about roles is that WordPress handles roles. BuddyPress doesn’t create or really use them that much.
the code you sent is the same in both message was it supposed to be different?
nevermind i see the ; you removed after roles but it still doesn’t work? Is this the entire code or are you using part of this?
function redirect_members() {
// redirect non logged-in members
if ( !is_user_logged_in() ) {
// get the ID of displayed member
$displayed_user = bp_displayed_user_id();
if (in_array($displayed_user,get_subscriber())) {
wp_redirect(site_url(‘/your-new-page-slug’));
exit();
}
}
}
add_action( ‘wp’, ‘redirect_members’, 1 );
// get all subscribers
function get_subscribers() {
$blogusers = get_users( ‘role=subscriber’ );
// Array of WP_User objects.
$exclude = array();
foreach ( $blogusers as $user ) {
$exclude[] .= esc_html( $user->ID );
}
return $exclude;
}
this code works perfectly to block the admin
// deny access to admins profile. User is redirected to the homepage
function bpfr_hide_admins_profile() {
global $bp;
if(bp_is_profile && $bp->displayed_user->id == 1 && $bp->loggedin_user->id != 1) :
wp_redirect( home_url() );
exit;
endif;
}
add_action( ‘wp’, ‘bpfr_hide_admins_profile’, 1 );
can’t we just change
if(bp_is_profile && $bp->displayed_user->id == 1 && $bp->loggedin_user->id != 1) :
wp_redirect( home_url() );
and make it subscribers in stead of id and loggedin d?
so I prematurely celebrated. So this does work however it also redirects the customer when the go to any pages like messages, buddydrive, ect;
I need to only block access to profile page but still allow them to visit the message or otheread tabs. I don’t use the standard buddypress design I just have custom links in the header alwso
members/username/messages
members/username/buddydrive
they are also redirected to homepage but I just need when they visit
members/username
Thanks again!
OK, that’s a different issue, ha ha. You’ll have to instead of preventing access to the entire user profile, prevent access to the user profile tab on the user’s profile. Clear, right?
You’ll need to use the Navigation API.
Navigation API
This should get you close: https://gist.github.com/dcavins/e7708f2a58aab16b61bbf7b06acf9756
There is no user profile tabs. I disable them by default. I just have links to the pages I want customer to view.
The code you gave me before works perfect. It’s just they need access to there other pages like messages buddy drive/ edit profile that stuff. Just not the main profile page?
subscribers only need to access
website.com/members/username/appointments
website.com/members/username/messages
website.com/members/username/shutterdrive/
website.com/members/username/profile/edit
it’s the subscriber who needs to be able to access these pages of there own . The contributor doesnt have access or need access
Dude again thanks so much for your help after like 5 hours ha I figure out the simple thing I needed to change
instead of if ( bp_is_user()
I changed it to
if ( bp_is_user_profile()
and now my other pages r visible how I need to subscribers and the subscriber profile is blocked.
2 beers and much streaming sports to solve this and again I thank you what’s your paypal email?
add_action( ‘wp’, function() {
if ( bp_is_user_profile() ) {
$user_meta = get_userdata( bp_displayed_user_id() );
if ( in_array( ‘subscriber’, $user_meta->roles ) ) {
wp_redirect( home_url() );
exit;
}
}
}, 1 );