# Lost Password Form on Your Own Page

> Adds a [wpide_lost_password] shortcode with a "reset your password" form and points WordPress's "Lost your password?" links to the page that holds it.

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

## Code

```php
// ID of the page that contains [wpide_lost_password].
$wpide_lost_password_page = 0;

add_filter( 'lostpassword_url', function ( $url, $redirect ) use ( $wpide_lost_password_page ) {
    if ( ! $wpide_lost_password_page || 'publish' !== get_post_status( $wpide_lost_password_page ) ) {
        return $url;
    }

    $page = get_permalink( $wpide_lost_password_page );

    return $redirect ? add_query_arg( 'redirect_to', rawurlencode( $redirect ), $page ) : $page;
}, 10, 2 );

add_action( 'template_redirect', function () {
    if ( empty( $_POST['wpide_lostpass_nonce'] ) ) {
        return;
    }

    if ( ! wp_verify_nonce( sanitize_key( $_POST['wpide_lostpass_nonce'] ), 'wpide_lostpass' ) ) {
        wp_die( 'The form expired. Please go back and try again.' );
    }

    $login = isset( $_POST['wpide_user_login'] ) ? sanitize_text_field( wp_unslash( $_POST['wpide_user_login'] ) ) : '';

    if ( '' !== $login ) {
        retrieve_password( $login ); // Errors are ignored so the form never reveals which accounts exist.
    }

    wp_safe_redirect( add_query_arg( 'reset-sent', 1 ) );
    exit;
} );

add_shortcode( 'wpide_lost_password', function () {
    if ( is_user_logged_in() ) {
        return '<p>You are already logged in.</p>';
    }

    if ( isset( $_GET['reset-sent'] ) ) {
        return '<p class="wpide-lostpass-notice" role="status">If an account matches, we have emailed a link to reset the password. Please check your inbox.</p>';
    }

    return '<form method="post" class="wpide-lostpass-form">'
        . '<p><label for="wpide-user-login">Username or email address</label><br><input type="text" id="wpide-user-login" name="wpide_user_login" autocomplete="username" required></p>'
        . wp_nonce_field( 'wpide_lostpass', 'wpide_lostpass_nonce', true, false )
        . '<p><button type="submit">Email me a reset link</button></p></form>';
} );
```

## Notes

Set $wpide_lost_password_page to the page ID. The reset link in the email still opens the secure WordPress screen to choose the new password.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Lost Password Form on Your Own 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/
