2015-10-14 145 views
1

我已經實現了基於AJAX的日誌功能在我的WP網站,具體如下:WordPress的AJAX登錄不重定向成功登錄

functions.php

function ajax_login_init(){ 

    wp_register_script('ajax-login-script', get_template_directory_uri() . '/ajax-login-script.js', array('jquery')); 
    wp_enqueue_script('ajax-login-script'); 

    wp_localize_script('ajax-login-script', 'ajax_login_object', array( 
     'ajaxurl' => admin_url('admin-ajax.php'), 
     'redirecturl' => home_url(), 
     'loadingmessage' => __('Sending user info, please wait...') 
    )); 

    // Enable the user with no privileges to run ajax_login() in AJAX 
    add_action('wp_ajax_nopriv_ajaxlogin', 'ajax_login'); 
} 

// Execute the action only if the user isn't logged in 
if (!is_user_logged_in()) { 
    add_action('init', 'ajax_login_init'); 
} 

function ajax_login(){ 

    // First check the nonce, if it fails the function will break 
    check_ajax_referer('ajax-login-nonce', 'security'); 

    // Nonce is checked, get the POST data and sign user on 
    $info = array(); 
    $info['user_login'] = $_POST['username']; 
    $info['user_password'] = $_POST['password']; 
    $info['remember'] = true; 

    $user_signon = wp_signon($info, false); 
    if (is_wp_error($user_signon)){ 
     echo json_encode(array('loggedin'=>false, 'message'=>__('Wrong username or password.'))); 
    } else { 
     echo json_encode(array('loggedin'=>true, 'message'=>__('Login successful, redirecting...'))); 
    } 

    die(); 
} 

ajax-login-script.js

jQuery(document).ready(function() { 

    // Perform AJAX login on form submit 
    jQuery('form#login_form').on('submit', function(e){ 
     jQuery('form#login_form p.login-status').show().text(ajax_login_object.loadingmessage); 
     jQuery.ajax({ 
      type: 'POST', 
      dataType: 'json', 
      url: ajax_login_object.ajaxurl, 
      data: { 
       'action': 'ajaxlogin', //calls wp_ajax_nopriv_ajaxlogin 
       'username': jQuery('form#login_form #txtEmail').val(), 
       'password': jQuery('form#login_form #txtPassword').val(), 
       'security': jQuery('form#login_form #security').val() }, 
      success: function(data){ 
       jQuery('form#login_form p.login-status').text(data.message); 
       if (data.loggedin == true){ 
        document.location.href = ajax_login_object.redirecturl; 
       } 
      } 
     }); 
     e.preventDefault(); 
    }); 

}); 

現在,當我在我的登錄彈出框中輸入錯誤的憑據,它成功顯示我等待的消息,然後錯誤Wrong username or password.。但是,當我使用正確的憑據並按登錄,然後它默默登錄我,但從來沒有刷新頁面(實際上它嘗試在後臺刷新頁面,但由於它是一個Ajax請求,我只能看到刷新console選項卡)。

我想讓ajax刷新我發送請求的當前頁面。

我認爲,罪魁禍首可能是這個片段:

$user_signon = wp_signon($info, false); 
if (is_wp_error($user_signon)){ 
      echo json_encode(array('loggedin'=>false, 'message'=>__('Wrong username or password.'))); 
     } else { 
      echo json_encode(array('loggedin'=>true, 'message'=>__('Login successful, redirecting...'))); 
     } 

$user_signon = wp_signon($info, false);被調用時,它實際上會記錄用戶和重定向頁面(內AJAX),從來沒有進一步執行代碼。因此,我沒有在我的AJAX成功函數中獲取JSON編碼數據(這是我實際嘗試使用JavaScript重定向頁面的地方)。

任何幫助將不勝感激。謝謝

+0

試着顛倒順序'如果(!is_wp_error(user_signon $))',也可反方向2回聲 – Mihai

+0

做到了,沒有運氣。 – Ali

回答

2

這是另一種方式。使用wp_check_password,wp_set_current_userwp_set_auth_cookie功能來實現相同的目標。

function ajax_login(){ 

    // First check the nonce, if it fails the function will break 
    check_ajax_referer('ajax-login-nonce', 'security'); 

    // Nonce is checked, get the POST data and sign user on 
    $info = array(); 
    $info['user_login'] = $_POST['email']; 
    $info['user_password'] = $_POST['password']; 
    $info['remember'] = true; 

    $userdata = get_user_by('login', $info['user_login']); 
    $result = wp_check_password($info['user_password'], $userdata->data->user_pass, $userdata->data->ID); 

    if ($result) { 

     auto_login($userdata); 
     echo json_encode(array('loggedin'=>true, 'message'=>__('Login successful, redirecting...'))); 

    } else { 

     echo json_encode(array('loggedin'=>false, 'message'=>__('Wrong username or password.'))); 

    } 

    die(); 

} 

function auto_login($user) { 

    if (!is_user_logged_in()) { 

     $user_id = $user->data->ID; 
     $user_login = $user->data->user_login; 

     wp_set_current_user($user_id, $user_login); 
     wp_set_auth_cookie($user_id); 

    } 
} 
+0

謝謝。它拯救了我的一天! –

0
<form class="well form-inline" id="login"> 
    <div id="message"></div> 
    <div id="loading" style="display:none;"></div> 
    <div class="rowmargin"> 
     <h4>Login</h4> 
    </div> 
    <div class="rowmargin"> 
     <input type="text" name="username" id="loginUsername" class="input-medium" placeholder="Username"> 
     <input type="password" name="password" id="loginPassword" class="input-medium" placeholder="Password"> 
    </div> 
    <a class="btn btn-primary" id="loginButton"><i class="icon-check icon-white"></i> Login</a> 
</form> 


jQuery(document).ready(function(){  
    jQuery('#loading').hide(); 
    jQuery("#loginButton").click(function() { 
     jQuery('#message').hide().html(''); 
     jQuery('#loading').hide(); 
     var input_data = jQuery('#login').serialize(); 
     var logUser = jQuery('#loginUsername').val(); 
     var logPass = jQuery('#loginPassword').val(); 

     if(logUser == '' && logPass != ''){ jQuery('#message').show().html('Your Username is empty!'); return false; } 
     if(logPass == '' && logUser != ''){ jQuery('#message').show().html('Your Password is empty!'); return false; } 
     if(logUser == '' && logPass == ''){ jQuery('#message').show().html('Your Username and Password is empty!'); return false; } 

     jQuery('#loading').show(); 
     jQuery.ajax({ 
      type: "POST", 
      url: "<?php echo site_url('wp-login.php','login_post'); ?>", 
      data: input_data, 
      success: function(msg) {          
       // login success. redirect users to some page. 
       jQuery(location).attr('href', '<?php echo home_url('/thank-you/'); ?>'); 

      }, 
      error: function(msg) { 
       // login error.     
       jQuery('#message').show(); 
       jQuery('#message').html("<?php _e('Your login is not correct. Please try again.'); ?>"); 
       jQuery('#loading').hide(); 
      } 

     }); 
     return false; 
    }); 
});