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%2FHere’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
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
BrajeshI 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
BrajeshHi Carsten,
Thank you for confirming. I am glad that it worked.Regards
BrajeshHi, 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!
Hi Lisa,
You will need to exclude the branded login pages from restricted scope.I will share the code in the day.
Regards
BrajeshHello 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
The topic ‘ [Resolved] Problem with 'Make a WordPress site Private code'’ is closed to new replies.