Wednesday 23 September 2015

forgot password with ajax in wordpress

step 1. add link for open bootstrap modal

 Forgot Password?



 

// add jquery and ajax call


 <?php
//finally add php code.

/*Forgot Password Start*/
        if(!empty($_POST['user_login']) && isset( $_POST['action'] ) &&  $_POST['action'] == 'reset') 
        {
         global $wpdb;
         $error = '';
         $success = '';
            $email = trim($_POST['user_login']);
            
            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 '

'. $error .'

'; if( ! empty( $success ) ) echo '

'. $success .'

'; } /*Forgot Password End*/ ?>

No comments:

Post a Comment