Wordpress custom Password recovery using ajax

0

I used the following code to create a custom form to retrieve the wordpress password via e-mail without having to go through the wordpress own form, here is the code -PHp -

<?php
    global $wpdb;

    $error = '';
    $success = '';


    if( isset( $_POST['action'] ) && 'reset' == $_POST['action'] ) 
    {
        $email = trim($_POST['user_login']);

        if( empty( $email ) ) {
            $error = 'Enter a username or e-mail address..';
        } else if( ! is_email( $email )) {
            $error = 'Invalid username or e-mail address.';
        } else if( ! email_exists( $email ) ) {
            $error = 'There is no user registered with that email address.';
        } else {

            $random_password = wp_generate_password( 12, false );
            $user = get_user_by( 'email', $email );

            $update_user = wp_update_user( array (
                    'ID' => $user->ID, 
                    'user_pass' => $random_password
                )
            );


            if( $update_user ) {
                $to = $email;
                $subject = 'Your new password';
                $sender = get_option('name');

                $message = 'Your new password is: '.$random_password;

                $headers[] = 'MIME-Version: 1.0' . "\r\n";
                $headers[] = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
                $headers[] = "X-Mailer: PHP \r\n";
                $headers[] = 'From: '.$sender.' < '.$email.'>' . "\r\n";

                $mail = wp_mail( $to, $subject, $message, $headers );
                if( $mail )
                    $success = 'Check your email address for you new password.';

            } else {
                $error = 'Oops something went wrong updaing your account.';
            }

        }

        if( ! empty( $error ) )
            echo '<div class="message"><p class="error"><strong>ERROR:</strong> '. $error .'</p></div>';

        if( ! empty( $success ) )
            echo '<div class="error_login"><p class="success">'. $success .'</p></div>';
    }
?>

-HTML -

    <form method="post">
        <fieldset>
            <p>Please enter your username or email address. You will receive a link to create a new password via email.</p>
            <p><label for="user_login">Username or E-mail:</label>
                <?php $user_login = isset( $_POST['user_login'] ) ? $_POST['user_login'] : ''; ?>
                <input type="text" name="user_login" id="user_login" value="<?php echo $user_login; ?>" /></p>
            <p>
                <input type="hidden" name="action" value="reset" />
                <input type="submit" value="Get New Password" class="button" id="submit" />
            </p>
        </fieldset> 
    </form>

This is functional, but the problem is when I use an include to add this to a page, because I use javascript to create login windows, registration and forgotten password. When you use the submit button to recover the password, the web loads and instead of seeing the php message, the web loads again and you go back to the login section without being able to see if you have placed a correct email or not, and I want to do it for ajax , the problem is that I have no idea how. Could someone guide me? Thanks and best regards

    
asked by Dario B. 10.07.2018 в 11:47
source

1 answer

0

at the end calmly, and reading a little, I managed to work as follows.

var registerbutton = $('#submit');
registerbutton.click(function (event) {
    event.preventDefault();
    var user_login = $('#user_login').val();
    var data = {
        user_login: user_login,
        action: 'reset'
    };
    $.ajax({
        type: "POST",
        dataType: 'text',
        url: "<?php echo get_template_directory_uri(); ?>/Password/password-recovery.php",
        data: data
    })
        .done(function (msg) {
            $("#result").html(msg);
        });
});

I leave it posted in case someone needs it.

    
answered by 11.07.2018 в 11:17