Any ideas what I’m doing wrong? Using over 4 years old tricks ? 😉
Give this a try (in bp-custom)
function bpfr_guest_redirect() {
global $bp;
// enter the slug or component conditional here
if ( bp_is_activity_component() || bp_is_groups_component() || bp_is_blogs_component() || bp_is_page( BP_MEMBERS_SLUG ) || is_bbpress() ) {
// not logged in user - user will be redirect to any link you want
if( !is_user_logged_in() ) {
//wp_redirect( get_option('siteurl') ); //back to homepage
//wp_redirect( get_option('siteurl') . '/register' ); // back to register page
wp_redirect( get_option('siteurl') . '/your_page_title' ); // back to whatever page
}
}
}
add_filter('get_header','bpfr_guest_redirect',1);
And to close the second entry door, which many users seems to ignore…
function bpfr_hide_rss_feed_to_nonreg_visitor() {
if ( !is_user_logged_in() ) {
remove_action( 'bp_actions', 'bp_activity_action_sitewide_feed' );
remove_action( 'bp_actions', 'bp_activity_action_personal_feed' );
remove_action( 'bp_actions', 'bp_activity_action_friends_feed' );
remove_action( 'bp_actions', 'bp_activity_action_my_groups_feed' );
remove_action( 'bp_actions', 'bp_activity_action_mentions_feed' );
remove_action( 'bp_actions', 'bp_activity_action_favorites_feed' );
remove_action( 'groups_action_group_feed', 'groups_action_group_feed' );
}
}
add_action('init', 'bpfr_hide_rss_feed_to_nonreg_visitor');
Thanks for the reply! So weirdly, this worked for a very brief moment when I tried to open a group page — but then it stopped redirecting. Member profiles continue to show no matter what. I wonder if there’s some kind of server config thing going on…
I attempted to use another similar solution I discovered on a recent thread (not 4 years old this time :-p) and still no luck. Here is that piece combined with the RSS backdoor protection:
<?php
function bp_guest_redirect( $name ) {
if( ! is_user_logged_in() ) {
if ( bp_is_activity_component() || bp_is_groups_component() || bp_is_blogs_component() || bp_is_directory() || bp_is_user() || bp_is_members_component() )
wp_redirect( get_option('siteurl') . '/wp-login.php/' );
}
}
add_action( 'get_header', 'bp_guest_redirect', 1, 1 );
function bpfr_hide_rss_feed_to_nonreg_visitor() {
if ( !is_user_logged_in() ) {
remove_action( 'bp_actions', 'bp_activity_action_sitewide_feed' );
remove_action( 'bp_actions', 'bp_activity_action_personal_feed' );
remove_action( 'bp_actions', 'bp_activity_action_friends_feed' );
remove_action( 'bp_actions', 'bp_activity_action_my_groups_feed' );
remove_action( 'bp_actions', 'bp_activity_action_mentions_feed' );
remove_action( 'bp_actions', 'bp_activity_action_favorites_feed' );
remove_action( 'groups_action_group_feed', 'groups_action_group_feed' );
}
}
add_action('init', 'bpfr_hide_rss_feed_to_nonreg_visitor');
?>
Both snippets (yours and mine) are working as expected.
Enable debug in wp-config; perhaps you could get some error message ?
You could also use this plugin. Exclude the URLs you want to be accessible to all. Everything else will be hidden, including from SERPS.
https://wordpress.org/plugins/jonradio-private-site/
For all private social network, but login and register page is available to access.
add_action( 'template_redirect', 'redirect_to_login_page_for_premium_members' );
function redirect_to_login_page_for_premium_members() {
if( (!is_page_template("loginpage.php")) && (!is_user_logged_in()) && (!is_page("Register") )) {
$page = get_page_by_title( 'Login' );
wp_redirect(get_permalink($page->ID));
exit();
}
}
Restrict buddypress components just for logged-in users:
*UNTESTED
add_action( 'template_redirect', 'redirect_to_login_page_for_premium_members' );
function redirect_to_login_page_for_premium_members() {
if( (is_buddypress()) && (!is_user_logged_in()) ) {
$page = get_page_by_title( 'Login' );
wp_redirect(get_permalink($page->ID));
exit();
}
}
Thank you all for the help!
Okay, here’s the bizarre thing — I created a new user account and logged out to test its functionality. After I did this, I got the new_member redirect page exactly ONE TIME for every single BP page. So one time, it would prevent non-logged-in me from accessing Recent Activity, but if I tried again, it would work from here on out. I think there must be something going on with respect to how often the function is being called, and perhaps how my server is cacheing sites. Should I try placing a function call somewhere else, like in the theme or somewhere that is likely to be checked every refresh? Thank you!
Zoë
I do not understand, what exactly is happening, should you be more clear?
Try to use twentytwelve theme and just add there snippets. See what happen. What is your theme?
Thank you.
Fixed it!! Okay, for anyone who encounters this issue, the problem was that for some reason (I think perhaps how WP or my server is caching pages) the hooks I was choosing to target for the behavior weren’t working. So instead I added both functions to the “wp” hook, and now it works site-wide!
/* You can add custom functions below, in the empty area
=========================================================== */
function bp_guest_redirect( $name ) {
if( ! is_user_logged_in() ) {
if ( bp_is_activity_component() || bp_is_groups_component() || bp_is_blogs_component() || bp_is_directory() || bp_is_user() || bp_is_members_component() )
wp_redirect( get_option('siteurl') . '/new_member/' );
}
}
add_action( 'wp', 'bp_guest_redirect', 1, 1 );
function bpfr_hide_rss_feed_to_nonreg_visitor() {
if ( !is_user_logged_in() ) {
remove_action( 'bp_actions', 'bp_activity_action_sitewide_feed' );
remove_action( 'bp_actions', 'bp_activity_action_personal_feed' );
remove_action( 'bp_actions', 'bp_activity_action_friends_feed' );
remove_action( 'bp_actions', 'bp_activity_action_my_groups_feed' );
remove_action( 'bp_actions', 'bp_activity_action_mentions_feed' );
remove_action( 'bp_actions', 'bp_activity_action_favorites_feed' );
remove_action( 'groups_action_group_feed', 'groups_action_group_feed' );
}
}
add_action('wp', 'bpfr_hide_rss_feed_to_nonreg_visitor');
My theme framework (WPZoom) also recommended I place theme modifying code in the functions/user/ pathway, so I modified functions.php there instead of using bp-custom. I think it would work either place, however.
Thank you!