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*/
 
?>

No comments:

Post a Comment