Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

Code

// 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 add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.