BuddyDev

Search

[Resolved] Make Buddypress Community Private

Tagged: 

  • Participant
    Level: Enlightened
    Posts: 78
    Jay Anderson on #331

    WP version 4.3
    Multisite Installation
    Category Style
    Buddypress installed on Main site
    Version 2.3.3
    BP Magic Theme Version 1.0.6

    Hello, I was wondering if there is any suggestion on how to make a buddypress site private. I am creating a high school community and would like to have all the pages, posts, etc. private except for the Home page, Login, Register, Lost Password.

    I’ve tried a couple of plugins but non seem to work with the BP Magic Theme.

    Any suggestions would be greatly appreicated.

    Jay

  • Keymaster
    (BuddyDev Team)
    Posts: 24190
    Brajesh Singh on #332

    Hi Jay,
    Thank you for asking.
    Just asking, are you comfortable with code? It can be done with a few lines of code, It has nothing to do with BP Magic theme but BuddyPress pages are not the actual pages(It resets data early), so most plugin fail at detecting that.

    If you are comfortable wit code, I will post a snippet quickly.

    Thank you
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 78
    Jay Anderson on #333

    Hi Brajesh,

    yeah I am ok with code. Not a master though.

    Thanks

  • Keymaster
    (BuddyDev Team)
    Posts: 24190
    Brajesh Singh on #339

    Hi Jay,
    Thank you. I will post the code in an hour.

  • Participant
    Level: Enlightened
    Posts: 78
    Jay Anderson on #359

    Great, thanks for the help. Looking forward to the code.

    Jay

  • Keymaster
    (BuddyDev Team)
    Posts: 24190
    Brajesh Singh on #360

    Hi Jay,
    Thank you.
    Please find the code below.

    you can put the code in your bp-custom.php or your theme’s functions.php (I will prefer bp-custom.php )

    
    /**
     * Make a site Private, works with/Withouthg BuddyPress
     * 
     * @author sbrajesh
     * @global string $pagenow
     * 
     */
    function buddydev_private_site() {
    	
    	//first exclude the wp-login.php
    	//register
    	//activate
    	global $pagenow;
    	
    	//do not restrict logged in users
    	if( is_user_logged_in() ) {
    		return ;
    	}
    	//if we are here, the user is not logged in, so let us check for exclusion
    	//we selectively exclude pages from the list
    	
    	//are we on login page?
    	if( $pagenow == 'wp-login.php' ) {
    		return ;
    	}
    	
    	
    	$exclude_pages = array( 'register', 'activate', 'alphabeta' );//add the slugs here
    	
    	//is it one of the excluded pages, if yes, we just return and don't care
    	if( is_page( $exclude_pages ) ) {
    		return ;
    	}
    	
    	$redirect_url = wp_login_url( site_url('/') );//get login url,
    	
    	wp_safe_redirect( $redirect_url );
    	exit( 0 );
    }
    
    add_action( 'template_redirect', 'buddydev_private_site', 0 );
    

    You can exclude any page by using the slug, It will work irrespective of the fact that BuddyPress may or may not be active.

    Please do check and let me know if that worked for you or not?

  • Participant
    Level: Enlightened
    Posts: 78
    Jay Anderson on #364

    Hi Brajesh,

    everything works great. Just one question…how do I add the Home Page to the exclude list? I can exclude all the other pages I need with the slug, but I’m not having any luck with the Home Page.

    Jay

  • Keymaster
    (BuddyDev Team)
    Posts: 24190
    Brajesh Singh on #365

    Hi Jay,
    I am sorry, I overlooked the homepage exclusion. I have updated the code below. Please update and check if this works.

    
    /**
     * Make a site Private, works with/Withouthg BuddyPress
     * 
     * @author sbrajesh
     * @global string $pagenow
     * 
     */
    function buddydev_private_site() {
    	
    	//first exclude the wp-login.php
    	//register
    	//activate
    	global $pagenow;
    	
    	//do not restrict logged in users
    	if( is_user_logged_in() ) {
    		return ;
    	}
    	//if we are here, the user is not logged in, so let us check for exclusion
    	//we selectively exclude pages from the list
    	
    	//are we on login page?
    	if( $pagenow == 'wp-login.php' ) {
    		return ;
    	}
    	
    	//let us exclude the home page
    	if( is_front_page() ) {
    		return ;
    	}
    	
    	$exclude_pages = array( 'register', 'activate', 'alphabeta', 'groups', 'members' );//add the slugs here
    	
    	//is it one of the excluded pages, if yes, we just return and don't care
    	if( is_page( $exclude_pages ) ) {
    		return ;
    	}
    	
    	$redirect_url = wp_login_url( site_url('/') );//get login url,
    	
    	wp_safe_redirect( $redirect_url );
    	exit( 0 );
    }
    
    add_action( 'template_redirect', 'buddydev_private_site', 0 );
    

    Thank you
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 78
    Jay Anderson on #369

    Hi Brajesh,

    works great.

    Thanks

The topic ‘ [Resolved] Make Buddypress Community Private’ is closed to new replies.

This topic is: resolved