BuddyDev

Search

[Resolved] Problem with 'Make a WordPress site Private code'

  • Participant
    Level: Yogi
    Posts: 1117
    calu on #32108

    Hi Brajesh, a while ago, you supplied me with this code, to make BP content private.
    Unfortunately I isolated a problem with WP login redirects to this code.

    So static pages like domain/support or domain/privacy-policy are redirected to the WP login page. There are no restrictions to these pages, and if i deactivate the code snippet, the links are working.

    This is how the redirection looks like:
    https://domain/login/?redirect_to=https%3A%2F%2Fdomain%2F

    Here’s the code:

    /**
     * Make a WordPress site Private, works with/Without BuddyPress
     *
     * @author sbrajesh
     * @global string $pagenow
     *
     */
    function buddydev_private_site() {
     
        //do not restrict logged in users
        if ( is_user_logged_in() ) {
            return ;
        }
        //first exclude the wp-login.php
        //register
        //activate
        global $pagenow;
     
        //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 ;
        }
        
        //handle Special case of BuddyPress registration/Login
        if ( function_exists( 'is_buddypress' ) && is_buddypress() ) {
     
            if ( bp_is_activation_page() || bp_is_register_page() ) {
                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 );
    
    Regards
    Carsten
    
  • Participant
    Level: Yogi
    Posts: 1117
    calu on #32110

    My question is, would it be possible to change this code, to make BP pages private, not WP pages in general?

  • Keymaster
    (BuddyDev Team)
    Posts: 24712
    Brajesh Singh on #32115

    Hi Carsten,
    Sure.
    Here is the code that make sthe BuddyPress section private.

    
    /**
     * Make a BuddyPress section of the site Private.
     *
     * @author sbrajesh
     * @global string $pagenow
     */
    function buddydev_private_site() {
    
    	// do not restrict logged in users.
    	if ( is_user_logged_in() ) {
    		return;
    	}
    	// first exclude the wp-login.php
    	// register
    	// activate.
    	global $pagenow;
    
    	// 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;
    	}
    
    	// handle Special case of BuddyPress registration/Login.
    	if ( function_exists( 'is_buddypress' ) && is_buddypress() ) {
    
    		if ( bp_is_activation_page() || bp_is_register_page() ) {
    			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 );
    
    

    Regards
    Brajesh

  • Keymaster
    (BuddyDev Team)
    Posts: 24712
    Brajesh Singh on #32117

    I just realized that we do not need the extra code from the first post since our goal has changed.

    You can restrict BuddyPress section by using the following code too

    
    
    /**
     * Make a BuddyPress section of the site Private.
     *
     * @author sbrajesh
     * @global string $pagenow
     */
    function buddydev_private_site() {
    
    	// do not restrict logged in users.
    	if ( is_user_logged_in() ) {
    		return;
    	}
    
    	// handle Special case of BuddyPress registration/Login.
    	if ( function_exists( 'is_buddypress' ) && is_buddypress() ) {
    
    		if ( bp_is_activation_page() || bp_is_register_page() ) {
    			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 );
    
    

    Regards
    Brajesh

  • Participant
    Level: Yogi
    Posts: 1117
    calu on #32242

    Hi Brajesh, thanks for the code, it works very well

    Regards
    Carsten

  • Keymaster
    (BuddyDev Team)
    Posts: 24712
    Brajesh Singh on #32247

    Hi Carsten,
    Thank you for confirming. I am glad that it worked.

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 65
    Lisa Lagrou on #45922

    Hi, I’m using the Branded Login plugin, and I’m getting too many redirects with this code. Can you give me a code to make the site private, but that works with the Branded Login plugin.

    Thanks!

  • Keymaster
    (BuddyDev Team)
    Posts: 24712
    Brajesh Singh on #45928

    Hi Lisa,
    You will need to exclude the branded login pages from restricted scope.

    I will share the code in the day.

    Regards
    Brajesh

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #45930

    Hello Lisa,

    Try the following code and let me know it helps or not

    
    /**
     * Make a BuddyPress section of the site Private.
     *
     * @author sbrajesh
     * @global string $pagenow
     */
    function buddydev_private_site() {
    
    	// do not restrict logged in users.
    	if ( is_user_logged_in() ) {
    		return;
    	}
    
    	// handle Special case of BuddyPress registration/Login.
    	if ( function_exists( 'is_buddypress' ) && is_buddypress() ) {
    
    		$skip = false;
    
    		if ( bp_is_activation_page() || bp_is_register_page() ) {
    			$skip = true;
    		} elseif ( function_exists( 'bp_branded_login' ) ) {
    			$skip = bl_is_bl_pages();
    		}
    
    		if ( $skip ) {
    			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 );
    
    

    Regards
    Ravi

  • Participant
    Level: Enlightened
    Posts: 65
    Lisa Lagrou on #45938

    Thank you! It works perfectly!

The topic ‘ [Resolved] Problem with 'Make a WordPress site Private code'’ is closed to new replies.

This topic is: resolved