Skip to:
Content
Pages
Categories
Search
Top
Bottom

[Resolved] Hide user profile fields & custom tabs from non-logged in users


  • mkricard
    Participant

    @mkricard

    Can anymore provide some guidance on hiding BuddyPress user profile fields and custom tabs from non-logged in users?

    I’ve been trying to figure out how to prevent non-logged in users from viewing my BuddyPress pages, but I can’t seem to quite make everything work. So far, I’ve been able to figure out how to restrict access to the Members page and redirect non-logged in users to the login page. I put the following code in my bp-custom.php file, which is in my plugins folder. I got the code from this BuddyPress forum page (https://buddypress.org/support/topic/hiding-groups-activity-members-list-to-non-members/).

    <?php
    
    /**
     * Change BuddyPress default Members landing tab.
     */
    define('BP_DEFAULT_COMPONENT', 'profile' );
    
    /* Prevent logged out users from accessing bp activity page */
    function nonreg_visitor_redirect() {
    global $bp;
    if ( bp_is_activity_component() || bp_is_groups_component() || bp_is_group_forum() || bp_is_page( BP_MEMBERS_SLUG ) ) {
    if(!is_user_logged_in()) { //just a visitor and not logged in
    wp_redirect( get_option('siteurl') . '/wp-login.php' );
    }
    }
    }
    add_filter('get_header','nonreg_visitor_redirect',1);
    
    ?>
    

    However, non-logged in users can still view each user’s page and their profile tabs. How can I restrict access to those pages as well? I’d rather not use another plugin.

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

  • danbp
    Moderator

    @danbp

    Your snippet use some deprecated code. (bp_is_page( BP_MEMBERS_SLUG ) since 1.8)

    Here a similar one who let you make BP almost private…

    function bpfr_guest_redirect() {
    global $bp;
    // enter conditional here
       if ( bp_is_activity_component() || bp_is_groups_component() || bp_is_blogs_component() ||  bp_is_members_component() || is_bbpress() ) {
    	
    // not logged in user - user will be redirect to any link to want
       if( !is_user_logged_in() ) { 
          //wp_redirect( get_option('siteurl') ); //back to homepage    			
          //wp_redirect( get_option('siteurl') . '/intranet' ); // back to whatever page 
          wp_redirect( get_option('siteurl') . '/register' ); // back to register page	
          } 
       }
    }
    add_filter( 'get_header', 'bpfr_guest_redirect', 1 );

    mkricard
    Participant

    @mkricard

    Thanks. I tried another piece of code in my bp-custom.php file, and it seemed to do the job perfectly. Now, non-logged in users can’t view any of my BP pages, and they get redirected to my register page:

    function kleo_page_template_redirect()
    {
        //if not logged in and on a bp page except registration or activation
        if( ! is_user_logged_in() && ! bp_is_blog_page() && ! bp_is_activation_page() && ! bp_is_register_page() ) {
            wp_redirect( home_url( '/register/' ) );
            exit();
        }
    }
    add_action( 'template_redirect', 'kleo_page_template_redirect' );
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘[Resolved] Hide user profile fields & custom tabs from non-logged in users’ is closed to new replies.
Skip to toolbar