# Send Logged-In Users Away From the Login Page

> When someone who is already logged in opens wp-login.php, they go straight to the dashboard (or the homepage for subscribers) instead of seeing the form again.

- Type: PHP
- Runs: Run everywhere
- Category: [Login](https://wpide.com/library/category/login/)
- Tags: login, redirect, ux
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/redirect-logged-in-from-login/

## Code

```php
add_action( 'login_init', function () {
    $action = isset( $_REQUEST['action'] ) ? sanitize_key( $_REQUEST['action'] ) : 'login';

    if ( 'login' !== $action || ! is_user_logged_in() || isset( $_REQUEST['interim-login'] ) || isset( $_REQUEST['reauth'] ) ) {
        return;
    }

    if ( 'POST' === ( isset( $_SERVER['REQUEST_METHOD'] ) ? $_SERVER['REQUEST_METHOD'] : 'GET' ) ) {
        return;
    }

    $fallback = current_user_can( 'edit_posts' ) ? admin_url() : home_url( '/' );
    $target   = isset( $_GET['redirect_to'] ) ? wp_validate_redirect( wp_unslash( $_GET['redirect_to'] ), $fallback ) : $fallback;

    wp_safe_redirect( $target );
    exit;
} );
```

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Send Logged-In Users Away From the Login Page" 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/
