09 March 2021

How to create forget password with otp in wordpress without plugin?

Create forget password with otp in wordpress without plugin.


 Main-Page


<?php

 /* Template Name: forget-password  */

 ob_start();

get_header();

?>


<div id="primary" class="content-area">

                <main id="main" class="site-main" role="main">

                                <?php

                                // Start the loop.

                                while ( have_posts() ) : the_post();

                                                // Include the page content template.

                                                get_template_part( 'template-parts/content', 'page' );

                                                // If comments are open or we have at least one comment, load up the comment template.

                                                if ( comments_open() || get_comments_number() ) {

                                                                comments_template();

                                                }

                                                // End of the loop.

                                endwhile;

                                ?>

                </main><!-- .site-main -->

           

</div><!-- .content-area -->

 




<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.js"></script>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.css">


<style>


.red_error{

color:REd;

}

</style>


<input type="hidden" name="user_id" class="user_id" val=''>

<form name="lostpasswordform" id="lostpasswordform" class="lostpasswordform" method="post">

<input type="email" name="email" class="email" placeholder="Email">

<div class="email_error red_error"></div>

<input type="tel" name="phone" class="phone" placeholder="Phone">

<div class="phone_error red_error"></div>

<input type="submit" name="forget_pass" class="forget_pass" value="Continue">


</form>


<?php


//echo get_option('admin_email');

?>


<script>

jQuery(document).ready(function($){

 

jQuery('.forget_pass').click(function(e) {

event.preventDefault()

var email = $('.email').val();

var phone = $('.phone').val();

if(email==''){

$('.email_error').html('Email field is required');

     return false;

    

     }

else{

$('.email_error').html(''); 

}

     if(phone==''){


$('.phone_error').html('Phone field is required');

     return false;

    

     }

else{

$('.phone_error').html('');

}

 

//alert(email+phone);

//var ajax = "http://wpcodekit.com/wp-admin/admin-ajax.php";

var ajax =  "<?php echo admin_url('admin-ajax.php'); ?>";


    var data = {

    'action' : "forget_password",

    'email': email,

    'phone' : phone

    }



     $.post(ajax,data,function(response){

    // console.log(response);

    // location.reload(true);

var result = $.parseJSON(response);


if(result[0] != ''){

$('.email_error').html(result[0]);

}

else{

$('.email_error').html('');

}

if(result[1] !=''){

$('.phone_error').html(result[1]);

}

else{

$('.phone_error').html('');

}

if(result[2]==1){

$('.lostpasswordform').html('<input type="text" name="opt" class="opt" placeholder="Enter Otp"><div class="otp_error red_error"></div><input type="submit" name="otp_bt" class="otp_bt" value="Continue">');

$('.user_id').val(result[3]);

}

else{

//$('.email_error').html('');

//$('.phone_error').html('');

}

//console.log(result[2]);

     });

});




jQuery(document).on('click','.otp_bt',function(e) {

e.preventDefault();

var otp = $('.opt').val();


if(otp==''){


$('.otp_error').html('OTP is required');

     return false;

    

     }

else{

$('.otp_error').html('');

}


 

//var ajax = "http://wpcodekit.com/wp-admin/admin-ajax.php";

var ajax =  "<?php echo admin_url('admin-ajax.php'); ?>";

    var data = {

    'action' : "check_otp",

    'otp': otp

    }


    $.post(ajax,data,function(response){

    //console.log(response);

    // location.reload(true);

  var result = $.parseJSON(response);

if(result[0] == 1){

$('.lostpasswordform').html('<input type="password" name="password" class="password" placeholder="New Password"><div class="password_error red_error"></div><input type="password" name="confirm_pass" class="confirm_pass" placeholder="Confirm New Password"><div class="confirm_error red_error"></div><input type="submit" name="sub_pass" class="sub_pass" value="Continue">');

$('.otp_error').html('');

}

else{

$('.otp_error').html(result[0]);

}


     });

});





jQuery(document).on('click','.sub_pass',function(e) {

e.preventDefault();

var password = $('.password').val();

var confirm_pass = $('.confirm_pass').val();

var user_id = $('.user_id').val();


if(password==''){

$('.password_error').html('Password is required');

     return false;

    

     }

else{

$('.password_error').html('');

}

 

if(confirm_pass==''){

$('.confirm_error').html('Confirm Password is required');

     return false;

    

     }

else{

$('.confirm_error').html(''); 

}


if(password!=confirm_pass){

$('.confirm_error').html('confirm password does not match');

return false;

     }

else{

$('.confirm_error').html('');

}


var ajax =  "<?php echo admin_url('admin-ajax.php'); ?>";


    var data = {

    'action' : "set_password",

    'password': password,

    'confirm_pass': confirm_pass,

    'user_id': user_id

    }


    $.post(ajax,data,function(response){

    //console.log(response);

    // location.reload(true);

  var result = $.parseJSON(response);

if(result[0] == 1){

$('.otp_error').html('');

swal("Success", "Well done, your password has been updated", "success")

}

else{


$('.confirm_error').html(result[0]);

}


     });

});

});


</script>


<?php 


get_footer(); 


?>



Paste in function.php



function send_mail($email,$message){


//php mailer variables

  $to = $email;

  $from = get_option('admin_email');

  $subject = "Otp";

  $headers = 'From: '. $from . "\r\n" .

    'Reply-To: ' . $from . "\r\n";

 


//Here put your Validation and send mail

$sent = wp_mail($to, $subject, strip_tags($message), $headers);


      if($sent) {

//echo 'email has been sent.';

      }//message sent!

      else  {

//echo 'Error';

      }//message wasn't sent

  

}



 function forget_password(){

 

ob_start();

$user_id;

$success_status = 0; 

if(function_exists('date_default_timezone_set')) {

date_default_timezone_set("Asia/Kolkata");

}

$date = date("d/m/Y");

$date1 =  date("H:i a");

$phone = $_POST['phone'];

$email = $_POST['email'];

global $wpdb;


if(!email_exists($email)){

$error_email = 'Email does not match, please recheck and try again';

$email_status = 0;

}

else{

$email_status = 1;

$error_email = '';

}


if($email_status == 0){

$error_phone = '';

$success_status = 0;

}

else{


$user = get_user_by('email',$email);

$user_id = $user->ID;


$havemobile = get_user_meta($user_id,'phone_no',$phone); // stores the value of logged in user's meta data for 'test'.


if ($havemobile==$phone){

   $error_phone = '';

else {

     $error_phone = 'Mobile does not match, please recheck and try again';

//die();

}


if(email_exists($email) && $havemobile==$phone){

$random_result = rand(10000,99999);

setcookie("forget_password_coo", $random_result, time() + (60 * 5)); //5min

send_mail($email,$random_result.' Valid OTP for 5-Min');

//echo $_COOKIE['forget_password_coo'].'jassi';

$success_status = 1; 

}

}

echo json_encode(array($error_email,$error_phone,$success_status,$user_id));

die();

//ob_end_clean();

 }


 add_action('wp_ajax_nopriv_forget_password','forget_password');

 add_action('wp_ajax_forget_password','forget_password');





 function check_otp(){

 

ob_start(); 

$otp = $_POST['otp'];

global $wpdb;

if($_COOKIE['forget_password_coo']==$otp){

//$otp_status = 'true';

$otp_status = 1;

}

else{

$otp_status = 'Please enter valid OTP';

}

echo json_encode(array($otp_status));

die();

//ob_end_clean();

 }


 add_action('wp_ajax_nopriv_check_otp','check_otp');

 add_action('wp_ajax_check_otp','check_otp');

 

 

 

 function set_password(){

 

ob_start(); 

$password = $_POST['password'];

$confirm_pass = $_POST['confirm_pass'];

$user_id = $_POST['user_id'];

global $wpdb;

if(!empty($user_id) && !empty($password)){

wp_set_password( $password, $user_id );

$password_status = 1;

}

else{

$password_status = 'error';

}


echo json_encode(array($password_status));

die();


 }


 add_action('wp_ajax_nopriv_set_password','set_password');

 add_action('wp_ajax_set_password','set_password');



No comments:

Post a Comment

How to create youtube videos slider with play and pause option in wordpress?

Create youtube videos slider with play and pause option in wordpress youtube videos slider Use this shortcode:- [punjab_today] function my_...