# Return Users to the Page They Were On After Login

> After logging in, visitors land back on the page they came from instead of the dashboard, which suits membership and community sites.

- Type: PHP
- Runs: Run everywhere
- Category: [Login](https://wpide.com/library/category/login/)
- Tags: login, redirect, members
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/login-return-to-previous-page/

## Code

```php
// Set to true to send administrators back to the previous page too (instead of the dashboard).
$wpide_return_admins_too = false;

add_action( 'login_form', function () {
    $referer = wp_get_referer();

    if ( ! $referer || false !== strpos( $referer, 'wp-login.php' ) || false !== strpos( $referer, '/wp-admin' ) ) {
        return;
    }

    $referer = wp_validate_redirect( $referer, '' );

    if ( $referer ) {
        echo '<input type="hidden" name="wpide_return_to" value="' . esc_attr( $referer ) . '">';
    }
} );

add_filter( 'login_redirect', function ( $redirect_to, $requested, $user ) use ( $wpide_return_admins_too ) {
    if ( ! ( $user instanceof WP_User ) || empty( $_POST['wpide_return_to'] ) ) {
        return $redirect_to;
    }

    if ( ! $wpide_return_admins_too && user_can( $user, 'manage_options' ) ) {
        return $redirect_to;
    }

    // Only override the default destination, never an explicit redirect_to.
    if ( '' !== $requested && admin_url() !== $requested ) {
        return $redirect_to;
    }

    return wp_validate_redirect( esc_url_raw( wp_unslash( $_POST['wpide_return_to'] ) ), $redirect_to );
}, 20, 3 );
```

## Notes

Some browsers or privacy settings do not send the referring page; those users get the normal destination.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Return Users to the Page They Were On After Login" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
