0

I'm building a custom page for password recovery...

I get the action "login_form_lostpassword" to redirect the custom page.

function new_lost_password_form()
{

    if ( 'GET' == $_SERVER['REQUEST_METHOD'] ) {
        if (is_user_logged_in()) {
            $this->redirect_logged_in_user();
            exit;
        }

        wc_get_template('page-templates/lost-password-form.php');
        exit;
    }
}
add_action('login_form_lostpassword', 'new_lost_password_form');

When i try recover a password everything works fine, But when an error occurs as the input blank the wordpress redirect to the default recovery page.

This happen why the wordpress reads the wp-login.php first...

As i will make to wordpress override the content of recovery page wich is in wp-login.php ?

The block must override is:

case 'lostpassword' :
case 'retrievepassword' :

    if ( $http_post ) {
        $errors = retrieve_password();
        if ( !is_wp_error($errors) ) {
            $redirect_to = !empty( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : 'wp-login.php?checkemail=confirm';
            wp_safe_redirect( $redirect_to );
            exit();
        }
    }

    if ( isset( $_GET['error'] ) ) {
        if ( 'invalidkey' == $_GET['error'] ) {
            $errors->add( 'invalidkey', __( 'Your password reset link appears to be invalid. Please request a new link below.' ) );
        } elseif ( 'expiredkey' == $_GET['error'] ) {
            $errors->add( 'expiredkey', __( 'Your password reset link has expired. Please request a new link below.' ) );
        }
    }

in the file wp-login.php

0

You just need to add your own action before any errors are returned, and if there is an error, redirect back to your page and display the error

add_action( 'lostpassword_post', 'smyles123_check_for_errors' );

function smyles123_check_for_errors( $errors ){

    if ( $errors->get_error_code() ){
        // REDIRECT BACK TO YOUR PAGE TO SHOW ERRORS
        // You can probably just append the error to the URL you redirect back to, but you need to set $redirect_to value
        wp_safe_redirect( $redirect_to );
        exit();
    }

}

You could also just create your own function to handle the password reset and just copy the code from wp-login.php

There's plenty of tutorials online to do this: https://code.tutsplus.com/tutorials/build-a-custom-wordpress-user-flow-part-3-password-reset--cms-23811

You can also look through the code on this plugin I created a while back that handles almost everything in relation to wp-login.php ... should have plenty of code information on handling custom errors, redirects, even rewrites:

https://github.com/tripflex/wp-login-flow

  • I'll read and study this material you sent, thanks very much! Thanks for answer! – Dayglor Nov 14 '16 at 1:05
  • You're welcome, please don't forget to accept it as the answer (if this was what you needed) – sMyles Nov 14 '16 at 1:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.