Redirect after Login, register and lost password on WordPress to original page where users came from

I am using wordpress and I want to change the redirect URL after login to wordpress to the same page where user logged in from. Currently I am using this code for logged in users:

function admin_default_page() {
  return '/new-dashboard-url';
}

add_filter('login_redirect', 'admin_default_page');

Instead of [new-dashboard-url], I want them to go back to the same page where they signed in from. How to do this?

Read More

Same thing for registration and password.

Related posts

Leave a Reply

2 comments

  1. If you want to redirect to the last page they were in, you can try something like this:

    add_filter('login_redirect', 'redirect_previous_page');
    
    function redirect_previous_page(){
        return $_SERVER["HTTP_REFERER"];
    }
    

    Seems that it can be done even easier:

    add_filter( 'login_redirect', 'wpse126853_redirect_to_request', 10, 3 );
    
    function wpse126853_redirect_to_request( $redirect_to, $request, $user ) {
        // instead of using $redirect_to we're redirecting back to $request
        return $request;
    }
    
  2. if ( (isset($_GET['action']) && $_GET['action'] != 'logout') || (isset($_POST['login_location']) && !empty($_POST['login_location'])) ) {
        add_filter('login_redirect', 'my_login_redirect', 10, 3);
        function my_login_redirect() {
            $location = $_SERVER['HTTP_REFERER'];
            wp_safe_redirect($location);
            exit();
        }
    }