Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sbrajesh/1105039 to your computer and use it in GitHub Desktop.
Save sbrajesh/1105039 to your computer and use it in GitHub Desktop.
Limit Site content view
//redirect non logged in users to homepage
function bp_guest_redirect() {
global $bp;
if(!is_user_logged_in()&&!(bp_is_page(BP_REGISTER_SLUG)||bp_is_page(BP_ACTIVATION_SLUG)||is_front_page())) { // not logged in user
wp_safe_redirect( bp_get_root_domain());//make sure page exists exist slug welcome
exit(0);//no further execution
}
}
add_action('template_redirect','bp_guest_redirect',1);
//redirect on first login to /private, it will redirect all users except admin
add_filter("login_redirect","bpdev_redirect_to_profile",100,3);
function bpdev_redirect_to_profile($redirect_to_calculated,$redirect_url_specified,$user){
/*if no redirect was specified,let us think ,user wants to be in wp-dashboard*/
if(empty($redirect_to_calculated))
$redirect_to_calculated=admin_url();
/*if the user is not super admin,redirect to his/her profile*/
if(!is_super_admin($user->user_login))
return get_bloginfo('url')."/private"
else
return $redirect_to_calculated; /*if site admin or not logged in,do not do anything much*/
}
@MikeAlan
Copy link

MikeAlan commented Mar 21, 2017

Hi Sbrajesh,

I am building a school alumni website and am using your 'redirect non logged in users to something' to redirect full members to their profile or a specific page (not decided yet) while none members are left to browse through the front and other pages.

I am getting the following error:
Notice: Undefined property: WP_Error::$user_login in /var/www/web/wp-content/themes/twentyseventeen-child/functions.php on line 49
As you see I have placed your script in functions.php within the twenty seventeen child theme.
Can you let me know why I might be getting this error and how to cure it please? I am using the most recent versions of Wordpress and BuddyPress.

Is there a more up to date version than the one you published 6 years ago, maybe incorporating something like:
'wp_redirect( '/group-members/' . bp_core_get_username( bp_loggedin_user_id() ));' to get the full BP dynamic menu?

If you require more info in support of the error mentioned above, please let me know.

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment