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 22 replies - 1 through 22 (of 22 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 );


    rammysons
    Participant

    @rammysons

    Hello,

    It seems like you’re working on a multi-author website and you’re looking for a way to control access to user profiles. Specifically, you’ve implemented code to deny access to administrators’ profiles, and you’re interested in extending this functionality to subscribers as well.

    To achieve this, you can modify the code you provided to include conditions for subscribers. Here’s an example of how you might achieve this:

    php
    Copy code
    // deny access to admins and subscribers profiles. Users are redirected to the homepage
    function bpfr_hide_admins_and_subs_profile() {
    global $bp;

    // Get the current user’s role
    $current_user = wp_get_current_user();
    $user_roles = $current_user->roles;

    // Check if the user is an admin or a subscriber
    if (bp_is_profile && $bp->loggedin_user->id != 1 &&
    (in_array(‘administrator’, $user_roles) || in_array(‘subscriber’, $user_roles))) :
    wp_redirect(home_url());
    exit;
    endif;
    }
    add_action(‘wp’, ‘bpfr_hide_admins_and_subs_profile’, 1);
    This code snippet checks if the currently displayed user’s role is either “administrator” or “subscriber,” and if the conditions are met, it redirects the user to the homepage.

    Remember to replace ‘administrator’ and ‘subscriber’ with the actual role names used in your WordPress setup.

    Thank you for reaching out, and a special shoutout to “TWITCHFOLLOWERFORGE“. If you have any further questions or need additional assistance, feel free to ask!

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