Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Forgot Password using ajax in wordpress

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.46k
    Comment on it

    Hello reader's, In this post we discussed about "Forgot Password using ajax in wordpress".

    In the below code, forgot password screen appear user fill their email address into the text box and click submit button, after that a new password will generate and it come into their email. Now user can use the new password for login.

    <?php
    if($_POST) {
    
        require_once('../../../wp-config.php');
        global $wpdb;
    
        // Change Password
        if( isset($_POST['forgot_submit']) && !empty($_POST['forgot_submit']) ) {
    
            if( $_POST['forgot_submit'] == 'Get New Password' ) { 
    
                $email_id = $_POST['email']; 
    
                if ( filter_var( $email_id, FILTER_VALIDATE_EMAIL ) ) {
                    // Get user data using email id
                    $user = get_user_by( 'email', $email_id );
                    if( isset($user) && !empty($user) ) {
    
                        $user_id = $user->ID;
                        $user_info = get_userdata($user_id);
                        $unique = sha1( rand(31, 8736985477967) );
    
                        update_user_meta($user_id, 'reset_password', $unique);                
    
                        $unique_url = site_url() .'/confirm-password/'. '?action=rp&key='. $unique .'&login='. $user_info->user_login;       
    
                        // Send email to user
                        add_filter( 'wp_mail_content_type',create_function('', 'return "text/html"; ') );
                        $to       = $email_id;
                        $subject  = "Kensington Clinic - Reset Password Link";
                        $message  = "<p>Hi ".ucfirst( $user_info->first_name ).",</p>";
                        $message .= "<p>Someone requested that the password be reset for the following account: : <a href='". site_url() ."'>". site_url() ."</a></p>";
                        $message .= "<p>Username:". $user_info->user_login ."</p>";
                        $message .= "<p>If this was a mistake, just ignore this email and nothing will happen.</p>";
                        $message .= "<p>To reset your password, visit the following address:</p>";
                        $message .= "<p><a href='". $unique_url ."'>". $unique_url ."</a></p>";
    
                        wp_mail( $to, $subject, $message );
    
                        $msg = "success";
    
                    }
                    else {
                        $msg = "Email id doesn't exist.";
                    }
                }
                else {
                    $msg = "Invalid email id.";
                }
                echo $msg;
            }
        }
    
        // Reset Password
        if( isset($_POST['reset_submit']) && !empty($_POST['reset_submit']) ) {       
    
            if($_POST['reset_submit']=='Reset Password') {
                $password  = trim($_REQUEST['password_reset']);
                $user_id   = trim($_REQUEST['uid']);
    
                if( isset($password) && !empty($password) ) {     
                    wp_set_password( $password, $user_id );
    
                    $user_info     = get_userdata($user_id);
    
                    // get user name
                    $login_data    = array();
    
                    $login_data['user_login']      = $user_info->user_login;
                    $login_data['user_password']   = $password;
    
                    $user_verify = wp_signon( $login_data, false );  
    
                    // user varification
                    if ( is_wp_error($user_verify) ) {
                        if( !empty( $user_info->user_login) ) {            
                            $msg = "Entered your password is wrong";
                        }
                        else {
                            $msg = 'Invalid User name';
                        }
                    }
                    else {
                        update_user_meta( $user_id, 'reset_password', 'vipul-reset' );
                        echo $msg = "success";
                    }
                }
    
            }
    
        }
    
    }
    
    ?>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: