Redirect users to the previous page they were on after login

Home Forums BulletProof Security Pro Redirect users to the previous page they were on after login

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #41134
    AITpro Admin
    Keymaster

    Email Question:

    How do I redirect users to to the page they were on after login?

    #41135
    AITpro Admin
    Keymaster

    Answer:

    // Redirects visitors to the login page if they are not logged in and if they are on either example-page-1, example-page-2 or example-page-3
    // After they login they will be redirected back to the page they were on before login.
    // The login URL will contain the URL to your login page + the redirect_to Query String with the slug for that page.
    // https://www.example.com/wp-login.php?redirect_to=http%3A%2F%2Fwww.example.com%2Fexample-page-1%2F
    function custom_login_redirect() {
         
    	if ( ! is_user_logged_in() && is_page( array( 'example-page-1', 'example-page-2', 'example-page-3' ) ) ) {
    		wp_safe_redirect( wp_login_url( get_permalink() ) ); 
    		exit();
    	}
    }
    
    add_action( 'template_redirect', 'custom_login_redirect' );
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.