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.

Similar Posts