Jquery WordPress前端AJAX登录和重定向
我有下面的代码,它工作得很好,允许用户在前端登录,并且可以重定向到主页 有人能解释一下需要做什么,以便我可以根据用户角色将用户重定向到不同的页面吗?例如,我希望管理员被重定向到WordPress仪表板,而另一个用户角色将被重定向到指定页面 函数文件:Jquery WordPress前端AJAX登录和重定向,jquery,ajax,wordpress,redirect,Jquery,Ajax,Wordpress,Redirect,我有下面的代码,它工作得很好,允许用户在前端登录,并且可以重定向到主页 有人能解释一下需要做什么,以便我可以根据用户角色将用户重定向到不同的页面吗?例如,我希望管理员被重定向到WordPress仪表板,而另一个用户角色将被重定向到指定页面 函数文件: function ajax_login_init(){ wp_register_script('ajax-login-script', get_template_directory_uri() . '/ajax-login-script.js',
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' => __('Verifying 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();
}
jQuery:
jQuery(document).ready(function($) {
// Perform AJAX login on form submit
$('form#login').on('submit', function(e){
$('form#login p.status').show().text(ajax_login_object.loadingmessage);
$.ajax({
type: 'POST',
dataType: 'json',
url: ajax_login_object.ajaxurl,
data: {
'action': 'ajaxlogin', //calls wp_ajax_nopriv_ajaxlogin
'username': $('form#login #username').val(),
'password': $('form#login #password').val(),
'security': $('form#login #security').val() },
success: function(data){
$('form#login p.status').text(data.message);
if (data.loggedin == true){
document.location.href = ajax_login_object.redirecturl;
}
}
});
e.preventDefault();
});
});