Wednesday, 23 September 2015

login with ajax in wordpress

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<div class="row">
<div class="col-md-7 border-right">
<form class="form-horizontal" id="login" method="post">
<fieldset>
  <style type="text/css">
  #danger{
    border: 1px solid red;height: 37px;text-align: center;background: red;color: white;
  }
  </style>
 <div class="status"></div>
 
  <input name="username" type="text" placeholder="Enter User Name" id="Logusername" class="form-control input-md" value="" required="">
 
  <input name="password" required="" type="password" placeholder="Enter Password" id="Logpassword" value="" class="form-control input-md">
   <div class="spacing"><input type="checkbox" name="checkboxes" id="checkboxes" value="1"><small> Remember me</small></div>
  <input id="Loginsubmit" type="button" value="Sign In" class="wpcf7-form-control wpcf7-submit">
</fieldset>
</form>
</div>
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// add jquery and ajax call
 
 
 
<script type="text/javascript">
   
jQuery(function(){
  jQuery('#Loginsubmit').on('click', function(){
    if(jQuery('#login').valid()){
    var submit = jQuery("#Loginsubmit");
    submit.attr("disabled", "disabled").addClass('disabled');
            content = {
                'action': 'login',
                'username': jQuery('#Logusername').val(),
                'password': jQuery('#Logpassword').val(),
                'remember': jQuery('#checkboxes').val()
            };
        jQuery.ajax({
            type: 'POST',
            url: 'http://your-website-url/page-name/',
            data: content,
            success: function(response){
              submit.removeAttr("disabled").removeClass('disabled');
                if ($($.parseHTML(response)).filter("#success").text()  == "Login successful, redirecting..."){
                    document.location.href = 'http://your-website-url/page-name/';
                }else if($($.parseHTML(response)).filter("#danger").text()  == "User is not activated. please go and activate"){
                    $('.status').html('<p id="danger">User is not activated. please go and activate.</p>').show();
                }
                else if($($.parseHTML(response)).filter("#danger").text()  == "Wrong username or password"){
                    $('.status').html('<p id="danger">Wrong username or password</p>').show();
                }
            }
        });
      }
  });
});
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
 
/*Start Login*/
 
if(!empty($_POST['username']) && !empty($_POST['password']) && $_POST['action'] == "login"){
    $info = array();
    $info['user_login'] = $_POST['username'];
    $info['user_password'] = $_POST['password'];
    $info['remember'] = $_POST['remember'];
    $user = get_userdatabylogin($_POST['username']);
    $CheckMeta = get_user_meta($user->ID, 'has_to_be_activated', true);
    $user_signon = wp_signon( $info, false );
    if ( is_wp_error($user_signon) ){
       echo  '<p id="danger">Wrong username or password</p>';
    }else if ($CheckMeta) {
        echo '<p id="danger">User is not activated. please go and activate</p>';
    }else {
        echo '<p id="success">Login successful, redirecting...</p>';
    }
}
 
/*End Login*/
 
?>

forgot password with ajax in wordpress

step 1. add link for open bootstrap modal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<a class="Forgot" data-toggle="modal" data-target="#myModal"><small> Forgot Password?</small></a>
 
<!--bootsrap modal html-->
 
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" data-keyboard="false" data-backdrop="static">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
       <div class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></div>
         <h4 class="modal-title" id="myModalLabel">Forgot Password</h4>
      </div>
      <div class="modal-body">
      <div id="ForgotMessage"></div>
       <form method="post" id="ForgotForm" action="">
        <fieldset>
            <p>Please enter your email address. You will receive a link to create a new password via email.</p>
            <p><label for="user_login">E-mail:</label>
                <input type="email" name="user_login" id="user_login" value="" required=""></p>
            <p>
                <input type="hidden" name="action" id="forgotaction" value="reset">
                <input type="button" value="Get New Password" class="button" id="Forgotsubmit">
            </p>
        </fieldset>
       </form>
       </div>
      </div>
    </div>
  </div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// add jquery and ajax call
<script type="text/javascript">
jQuery(function(){
jQuery('#Forgotsubmit').on('click', function(){
    if(jQuery('#ForgotForm').valid()){
    var submit = jQuery("#Forgotsubmit");
     submit.attr("disabled", "disabled").addClass('disabled');
    contents = {
      action: jQuery('#forgotaction').val(),
      user_login: jQuery('#user_login').val()
    };
    jQuery.ajax({
      type: 'POST',
      data: contents,
      success: function(response){
        submit.removeAttr("disabled").removeClass('disabled');
        jQuery('#user_login').val('');
          if ($($.parseHTML(response)).filter(".error").text()  == "Invalid username or e-mail address."){
              $('#ForgotMessage').html('<p id="error">Invalid username or e-mail address.</p>');
          }else if($($.parseHTML(response)).filter(".error").text()  == "There is no user registered with that email address."){
              $('#ForgotMessage').html('<p id="error">There is no user registered with that email address.</p>');
          }else if($($.parseHTML(response)).filter(".done").text()  == "Check your email address for you new password."){
              $('#ForgotMessage').html('<p id="done">Check your email address for you new password.</p>');
          }else if($($.parseHTML(response)).filter(".error").text()  == "Oops something went wrong updaing your account."){
              $('#ForgotMessage').html('<p id="error">Oops something went wrong updaing your account.</p>');
          }
      }
    });
  }
  });
});
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?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 '<p class="error">'. $error .'</p>';
             
            if( ! empty( $success ) )
                echo '<p class="done">'. $success .'</p>';
        }
 
/*Forgot Password End*/
 
?>

how to use number_format in php

1
2
3
4
5
6
7
8
<?php
$Price = 8945.234564;
$Price = number_format($UsedPrintPrice, 2);
print $Price;
?>
 
//: Output
8945.23

Rounding to the nearest nickel in php

1
2
3
4
5
6
7
<?php
$Price = 98.06;
$Price = round(20 * $Price,0)/20;
print $Price;
?>
//: Output
98.05

Saturday, 19 September 2015

str_replace in php

Replace characters from a variable using str_replace.
1
2
3
4
5
6
<?php
$Article = "This is my first paragraph.";
echo str_replace('paragraph' ,'Article', $Article);
?>
//: Output
This is my first Article.