Skip to:
Content
Pages
Categories
Search
Top
Bottom

Reply To: Disallow Non logged users on certain pages

@mrjarbenne

Participant

This is what I use. It blocks access to all the BP and bbPress components, but allows visitors to see the blog. I’m sure you could customize it to kill access to that too.

When you attempt to access a page you shouldn’t be able to see, you are redirected back to the home page

/**
 * Block public access to site with some exceptions.
 */
function hwdsb_block_access() {
	if ( is_user_logged_in() ) {
		return;
	}

	$redirect = is_buddypress();

	$site_path = bp_core_get_site_path();

	// subdirectory install
	if ( is_multisite() && ! is_subdomain_install() ) {
		global $current_blog;
		$site_path = $current_blog->path;
	}

	// strip site path from URI
	$path = substr( esc_url( $_SERVER['REQUEST_URI'] ), strlen( $site_path ) );

	if ( ! $redirect && function_exists( 'is_bbpress' ) ) {
		$redirect = is_bbpress();
	}

	// redirect if we match a condition and we're not on the homepage
	if ( (bool) $redirect === true && $path != '' ) {
		bp_core_redirect( bp_core_get_root_domain() );
		exit();
	}

}
add_action( 'template_redirect', 'hwdsb_block_access', 0 );
Skip to toolbar