Functional code written using PHP 5.3+ Closures. Read the comments throughout to understand concept and flow.
// Hook right before Themes kick in
add_action('wp', function(){
// Allow viewing of home/front_page
if(is_home() or is_front_page()) return;
// Maybe allow access to archives (non singulars) to?
// (when you hide important content after <!--more-->)
// if(is_archive()) return; // Uncomment to allow access to archives
// Allow access to BuddyPress registration/login page (experimental)
if(is_callable('bb_get_location')){
if(preg_match('~^(login|registration)-page$~i', bb_get_location())){
return; // Allow access
}
}
// Skip BuddyPress registration page directly (slightly experimental)
if(is_page() and is_array($bp_pages = get_option('bp-pages'))){
$current_page = get_queried_object_id();
// Allow direct access to registration page
if(!empty($bp_pages['register']) and ($bp_pages['register'] == $current_page)){
return; // Bail on registration page
}
// If you also want access to other special BuddyPress pages do this:
if(in_array($current_page, $bp_pages)){
// return; // Uncomment me to allows access to them
}
}
// Allow access to BBPress registration/login page (experimental)
if(is_callable('bbp_get_query_name')){
if(preg_match('~^bbp_(login|register)$~i', bbp_get_query_name())){
return; // Allow access
}
}
// If user is logged it let it be
if(is_user_logged_in()) return;
// Block Robots using X-Robots-Tag: header
// (no point in allowing Robots to index this)
if(!headers_sent()){ // Be defensive
header('X-Robots-Tag', 'noindex, nofollow, noarchive');
}
// Redirect to Login URL here
wp_redirect(wp_login_url($_SERVER['REQUEST_URI']), 302);
die; // Bail here
});
// And add some Memberhsip info on wp-login.php
add_action('login_footer', function(){
// Print some information here about Membership and site
echo '<p style="text-align: center;">', 'Awesome Memberhsip stuff.', '</p>';
});
// Block indexing of login page
add_action('login_init', function(){
// Block Robots using X-Robots-Tag: header
if(!headers_sent()){ // Be defensive
header('X-Robots-Tag', 'noindex, nofollow, noarchive');
}
});
Regards.