Skip to main content
WordPress made easy with the drag & drop Total WordPress Theme!Learn More

Redirect WordPress Users After Login to the Previous Page

Last modified: September 25, 2023

By default in WordPress when a user logs into a website via the main WordPress login screen (wp-login.php) they are redirected to the WP admin. This guide will show you how to change the default redirection so users are sent to the previous page they were on.

Why Should You Redirect Users?

For most sites redirecting to the WP dashboard after the user logins in probably makes the most sense, however this may not always be the case.

For example if you disable access to the admin side you may want your users redirected to an “account” page on the front-end or perhaps you just want users to be redirected to the previous page they were on if your website offers some sort of premium content that can only be viewed while logged in.

You may also have a membership style website where some users are able to access the WP admin while other users can not. So you will want to re-direct your site members to a different page. Below you will find 2 snippets: One for redirecting all users and another one for redirecting only certain users based on their role.

How to Redirect All Users?

The following snippet can be used to redirect all users after they successfully log in to your site. As you can see this snippet uses the “wp_get_referrer” function which returns the previous/referring page.

/**
 * Redirect all users after login to the previous page.
 *
 * @link https://www.wpexplorer.com/redirect-wordpress-users-login-previous-page
 */
add_filter( 'login_redirect', function( $redirect_to, $requested_redirect_to, $user ) {
    if ( ! $requested_redirect_to ) {
        $referrer = wp_get_referer();

        // Make sure the referring page is not a variation of the wp-login page or was the admin (aka user is logging out).
        if ( $referrer && ! str_contains( $referrer, 'wp-login' ) && ! str_contains( $referrer, 'wp-admin' ) ) {
            $redirect_to = $referrer;
        }
    }
    return $redirect_to;
}, 10, 3 );

Snippet requires PHP 8.0+ to support older versions add a polyfill for the str_contains function to your site. We recommend updating your PHP version though.

Redirect Specific Users

Now, if you only want to redirect some users then you will want to modify the snippet above to get the current user’s role and then redirect them only if their role matches the roles in your allowed list. This is useful to prevent redirecting your admins if you want the admins to be redirected to the WP dashboard.

The example below will redirect subscribers to the referring page and all other user roles will be redirected to the WP admin. If you want to redirect others users besides subscribers you can modify the “users_to_redirect” array to include any roles.

/**
 * Redirect certain users after login to the previous page.
 *
 * @link https://www.wpexplorer.com/redirect-wordpress-users-login-previous-page
 */
add_filter( 'login_redirect', function( $redirect_to, $requested_redirect_to, $user ) {
    if ( ! $requested_redirect_to ) {
        $referrer = wp_get_referer();

        // Make sure the referring page is not a variation of the wp-login page or was the admin (aka user is logging out).
        if ( $referrer && ! str_contains( $referrer, 'wp-login' ) && ! str_contains( $referrer, 'wp-admin' ) ) {
            $redirect_to = $referrer;
        }
    }

    // Restore original redirection @see wp-login.php
    if ( is_a( $user, 'WP_User' ) && $user->exists() && $user->has_cap( 'manage_options' ) ) {

        // Modify this to be an array of the users you want to redirect.
        $users_to_redirect = [
            'subscriber',
        ];

        $roles = (array) $user->roles;
        if ( $roles && ! array_intersect( $roles, $users_to_redirect ) ) {
            $redirect_to = admin_url();
        }
    }
    return $redirect_to;
}, 10, 3 );

Code not working? If either of the snippets above are not working on your site, double check your child theme and 3rd party plugins to make sure none of them are conflicting. You may also want to clear your browser cache just incase previous redirections were cached.

Below are links to the WordPress Codex with further information about the hooks and functions used in the tutorial.

redirect-wordpress-users-login-previous-page
Article by Kyla WPExplorer.com staff

2 Comments

  1. Nick

    Thank you for the code snippet! that’s exactly what I was looking for. It’s working flawlessly with users who simply need to log in.

    However, it causes a loop for users who requested a password reset. They get the error message “Your password reset link appears to be invalid. Please request a new link below.” “/wp-login.php?action=lostpassword&error=invalidkey”

    As soon as I deactivated the snippet, everything went back to normal. Did you run into a similar issue? Could you try and reproduce this problem on your side, when you try and log in again, after requesting a password reset?

    Thank you!

    • AJ Clarke

      Oh, I hadn’t thought about that. Thanks for the heads up! I usually disable the lost password function on my sites and forgot to check for that. But I can see why it would cause issues because wp_get_referer() will always return the last page and in this case it will be the lost password page.

      You’ll need to add extra checks to see what the referring page is and perhaps not redirect if the previous page is not the same as the login page. I’ve updated my snippet above to add some extra checks.

      Thanks for the heads up!

Leave a Reply

Your email address will not be published. Required fields are marked *

Learn how your comment data is processed by viewing our privacy policy here.