# Use a Custom Page for Logging In

> Sends visitors who open wp-login.php to your own login page (for example one with the login form shortcode), and back there when a login fails.

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

## Code

```php
// Path of your login page. It must contain a login form, e.g. the [wpide_login_form] shortcode.
$wpide_login_page_path = '/login/';

add_action( 'login_init', function () use ( $wpide_login_page_path ) {
    $action = isset( $_REQUEST['action'] ) ? sanitize_key( $_REQUEST['action'] ) : 'login';
    $method = isset( $_SERVER['REQUEST_METHOD'] ) ? strtoupper( sanitize_key( $_SERVER['REQUEST_METHOD'] ) ) : 'GET';

    // Leave logout, password reset, registration, re-authentication and form posts on wp-login.php.
    if ( 'login' !== $action || 'GET' !== $method || isset( $_GET['interim-login'] ) || isset( $_GET['reauth'] ) || isset( $_GET['loggedout'] ) || isset( $_GET['checkemail'] ) ) {
        return;
    }

    $target = home_url( $wpide_login_page_path );

    if ( ! empty( $_GET['redirect_to'] ) ) {
        $target = add_query_arg( 'redirect_to', rawurlencode( wp_validate_redirect( wp_unslash( $_GET['redirect_to'] ), admin_url() ) ), $target );
    }

    wp_safe_redirect( $target );
    exit;
} );

add_action( 'wp_login_failed', function () use ( $wpide_login_page_path ) {
    $referer = wp_get_referer();

    if ( ! $referer || false === strpos( $referer, $wpide_login_page_path ) || 'wp-login.php' !== ( isset( $GLOBALS['pagenow'] ) ? $GLOBALS['pagenow'] : '' ) ) {
        return;
    }

    wp_safe_redirect( add_query_arg( 'login', 'failed', home_url( $wpide_login_page_path ) ) );
    exit;
} );
```

## Notes

Create the page first and test logging in from a private window before you rely on it.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Use a Custom Page for Logging In" 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/
