Question

I have a simple problem. I need to create a front end page that will handle reset password function using wordpress, so basically it is same with the wordpress default one except that it is in front end.

But there is one thing that I could not figure out, when a user reset the password in front end, but the username or email he or she input is not exist in database, so now we need to give an error message. currently if this happened, it will redirect to admin wp-login.php, this is not good, I want it to redirect to a front end page with a parameter and user should never see an admin page

is there any solution for this, this could be simple, but I have been searching for a while and couldn't find an answer

thanks very much for any help

Was it helpful?

Solution

well let me answer my question, basically use "lostpassword_post" to validate the username or password before reset post is sent and then if error, redirect.

add_action('lostpassword_post', 'validate_reset', 99, 3);

function validate_reset(){
    if(isset($_POST['user_login']) && !empty($_POST['user_login'])){
        $email_address = $_POST['user_login'];
        if(filter_var( $email_address, FILTER_VALIDATE_EMAIL )){
            if(!email_exists( $email_address )){
                wp_redirect( 'register/?userexist=false' );
                exit;
            }
        }else{
                $username = $_POST['user_login'];
                if ( !username_exists( $username ) ){
                    wp_redirect( 'register/?userexist=false' );
                    exit;
                }
            } 

    }else{
        wp_redirect( 'register/?lostempty=true' );
        exit;   
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top