Making a WordPress & BuddyPress Site Private, the Right Way
Do you plan to make a WordPress & BuddyPress site private? Most of the plugins that claim to make a site private fail with BuddyPress activated. The problem is BuddyPress resets page ids for the pages used by BuddyPress. The simple solution that works with BuddyPress as well as WordPress is to hook into template_redirect action early.
Here is a snippet that prevents the users from viewing BuddyPress site content. We have excluded login page, register page, activation page and home page from the list.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | /** * 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 ); |
You can put the above code in your theme's functions.php or bp-custom.php in the plugins directory.
Hope that helps. Feel free to modify it and use it for your specific purpose.
Thank you for this snippet! Finally something that works. Question, though: How would you exclude blog-posts from becoming private? Meaning – keep blog-posts visible to the public?
Thank you Maite,
Glad it worked.
You can add the following lines before line 32 above
and that will do it.
Best Regards
Brajesh
Hi Brajesh,
I would like to make every directory private in our Buddypress site. The only publicly accessible page is the custom login page..When the user goes to our homepage https://narrativeatlas.org…they are presented with the login page..Using force login plugin…It works fine…..however, I can still see files on the site while logged out. Would like to make all invisible to non users
https://narrativeatlas.org/wp-content/uploads/2018/09/Screen-Shot-2018-09-05-at-1.49.55-PM.png
Hi Brajesh. Any work on this?
Restricting wp-content/uploads and showing uploaded file via PHP is not efficient and not recommended.
Regards
Brajesh