top of page

Forget Password in WordPress

  • Writer: سُلَيْمَان بْن دَاوُوْد
    سُلَيْمَان بْن دَاوُوْد
  • Jun 30, 2020
  • 1 min read

AJAX call in JavaScript

      jQuery('#forgetpassbutton').click(function(e){
        e.preventDefault();
        jQuery.ajax({
            type: 'POST',
            url : code.ajaxurl,
            data: { 
                    'action'        : 'send_forgetpassword_mail',
                    'memberid'      : jQuery('#useremail').val()
            },
            success: function(response){
                responseobj = jQuery.parseJSON(response);
                jQuery('#message').text(responseobj['message']);
                
            },
            error: function (data) {
            
            }
        });
      });

Add this function to functions.php

add_action('wp_ajax_send_forgetpassword_mail','send_forgetpassword_mail_callback');
add_action('wp_ajax_nopriv_send_forgetpassword_mail','send_forgetpassword_mail_callback');
function send_forgetpassword_mail_callback(){
	$useremail    = sanitize_text_field($_POST['memberid']);
	$userdetails  = get_user_by(  'email', $useremail );
	$user_id      = $userdetails->data->ID;
	if($userdetails){
		$password    = wp_generate_password(6,true); 
		wp_set_password(  $password,  $user_id );
		$subject      = 'Updated Account Details for psychodramacertification.org'  ;
		$message = '<table>
							<tr><th colspan="2">Updated Account Details</th></tr>
							<tr><td>Login URL</td>:<td>'.site_url('member-login').'</td></tr>
							<tr><td>Username</td>:<td>'.$userdetails->data->user_login.'</td></tr>
							<tr><td>Password</td>:<td>'.$password.'</td></tr>
						</table>';
		$headers = array('Content-Type: text/html; charset=UTF-8','Reply-To: Admin <'.get_option('admin_email').'>','Cc:'.get_option( 'mm_global_email_setting'));
		wp_mail($useremail , $subject, $message, $headers);
	}
	if($password){
	 	echo json_encode(array('flag' => 'success','message' => 'Your new password has been sent to your registered email address.','redirecturl' => '' ));
	}else{
		echo json_encode(array('flag'=>'failure','message'=> 'User account is not activated.' ));
	}
	die; 
}

Recent Posts

See All
Hide Upcoming Events

Hiding Upcoming Events can be relatively useful when you are using ACF fields to show Dates for an an Event The Following Snippet is an...

 
 
 

Comments


© 2020 by syednazrulhassan

bottom of page