Search code examples
wordpressauthenticationhttp-redirectbuddypress

WordPress Redirect for Non-Logged In


Ok so I have the code working for redirecting a logged in user to their BuddyPress profile; however, I also need:

QUESTION HOW TO:

Add a redirect for the same page #78 for a non-logged in user to go to: /login

function buddyforms_redirect_a_page_to_the_buddypress_profile() {
    if( is_page( 78 ) ) {
        if(is_user_logged_in()){
            wp_safe_redirect( bp_loggedin_user_domain() );
            exit;
        }
    }
}
add_action( 'wp', 'buddyforms_redirect_a_page_to_the_buddypress_profile' );

Solution

  • Check my example below. I have added the else condition for non-logged in user.

    function buddyforms_redirect_a_page_to_the_buddypress_profile() {
        if( is_page( 78 ) ) {
            if(is_user_logged_in()){
                wp_safe_redirect( bp_loggedin_user_domain() );
                exit;
            } else {
                wp_safe_redirect( wp_login_url() );
                exit;
            }
        }
    }
    add_action( 'wp', 'buddyforms_redirect_a_page_to_the_buddypress_profile' );