Hide Buddypress member pages from non-logged-in users and search engines

Sometimes you need to restrict Buddypress member pages from search engines and/or users who are not logged in. There are a few ways to solve this, but the easiest method is to require users to be logged in to view member pages. To do that, just drop this code into your functions.php file.

function k4media_buddypress_member_pages_login_check() {
	if( bp_is_user() && ! is_user_logged_in() ) {
		auth_redirect();
	}
}
add_action( 'template_redirect', 'k4media_buddypress_member_pages_login_check' ); 

The code is pretty simple. The function bp_is_user() checks to see if the user is viewing a Buddypress member page. The function is_user_logged_in() checks to see if, well, it’s pretty obvious what it checks for, right? The exclamation point means “not”. In human-friendly terms, the line of code reads like this: if the user is viewing a Buddypress member page and the user is not logged in, then auth_redirect(), which is a built-in WordPress function that sends users to the login page.