BuddyDev

Search

[Resolved] Making a WordPress & BuddyPress Site Private, the Right Way issue

  • Participant
    Level: Yogi
    Posts: 1105
    calu on #48665

    Hi there, taking up this old topic from here

    https://buddydev.com/support/forums/topic/problem-with-make-a-wordpress-site-private-code/

    as I discovered an issue which is related to both the revised code below and the original code from here.
    https://buddydev.com/making-a-wordpress-buddypress-site-private-the-right-way/

    Here’s the case:

    When a user has not been active on the page for a while, the user is logged out, I think this is the way WP works.

    The problem occurs when the user wants to access the page from members area again, with the last url used, eg. domain.com/search or domain.com/members/profile.

    Without the code, the user will be linked to the frontpage to login again, but with the code, the url is not cleared when the page is refreshed, and the user gets the “Too many redirections” error, and can’t access without clearing the url manually.

    So the code snippet prevent redirection to the front page, when users are automatically logged out.

    Can something be added to the code, so the logged out user is redirected to the frontpage?

    Regards
    Carsten

    /**
     * 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 );
    
  • Keymaster
    (BuddyDev Team)
    Posts: 24262
    Brajesh Singh on #48674

    Hi Carsten,

    You are most probably using a plugin that is not using wp-login.php for login. The code will work fine if you are using normal WordPress login page.

    My assumption is you are using a custom front end page for long and if that is the case, that page needs to be skipped to avoid infinite redirect.

    Regards
    Brajesh

  • Participant
    Level: Yogi
    Posts: 1105
    calu on #48682

    Hi Brajesh, thanks for your feedback, it lead me to look at WPS Hide Login plugin, which turned out to be causing the issue, used together with the code.

    The problem is now that I need both. I can alternatively use BP Registration Options plugin which has a Private Network which works with WPS Hide Login.

    Regards
    Carsten

  • Keymaster
    (BuddyDev Team)
    Posts: 24262
    Brajesh Singh on #48701

    Hi Carsten,
    Thank you for sharing the details.

    If you can ask WPS Hide Login team if there is any function to check we are on their login page, then I can certainly update the code to skip that page.

    That will fix it.

    Regards
    Brajesh

You must be logged in to reply to this topic.

This topic is: resolved