Skip to:
Content
Pages
Categories
Search
Top
Bottom

deny access to subscriber profile


  • giilour
    Participant

    @giilour

    hi – I have a multi author website and want people to be able to view authors profiles. I also have subscribers – is there a way to deny access for people to view subscribers profiles even if they type in the full web address and username?

    I found this code for admins and am using it atm – is there a way to have it for subs as well.

    // 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 );

Viewing 21 replies - 1 through 21 (of 21 total)

  • kwavewd
    Participant

    @kwavewd

    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.


    kwavewd
    Participant

    @kwavewd

    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 );
    

    kwavewd
    Participant

    @kwavewd

    I added your code to my functions.php page and all I got was a white screen?


    kwavewd
    Participant

    @kwavewd

    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?


    kwavewd
    Participant

    @kwavewd

    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.


    kwavewd
    Participant

    @kwavewd

    still white screen


    kwavewd
    Participant

    @kwavewd

    the code you sent is the same in both message was it supposed to be different?


    kwavewd
    Participant

    @kwavewd

    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;
    }


    kwavewd
    Participant

    @kwavewd

    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?


    kwavewd
    Participant

    @kwavewd

    any additional help?

    Ha, bp_is_profile() isn’t a function. This works:
    https://gist.github.com/dcavins/c5bb41691846b809c30c72230c3c5adf


    kwavewd
    Participant

    @kwavewd

    YES!!!!!!!!!!!

    Your are the MAN!!!!!

    THANK YOU SO MUCH!


    kwavewd
    Participant

    @kwavewd

    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


    kwavewd
    Participant

    @kwavewd

    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?


    kwavewd
    Participant

    @kwavewd

    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


    kwavewd
    Participant

    @kwavewd

    it’s the subscriber who needs to be able to access these pages of there own . The contributor doesnt have access or need access


    kwavewd
    Participant

    @kwavewd

    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?


    kwavewd
    Participant

    @kwavewd

    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 );

Viewing 21 replies - 1 through 21 (of 21 total)
  • You must be logged in to reply to this topic.
Skip to toolbar