I logged in my site with custom ajax. Then try to connect my wp-admin, the page redirect me to login.php
My custom ajax function in php;
function ajax_login(){
check_ajax_referer( 'ajax-login-nonce', 'security' );
$info = array();
$info['user_login'] = $_POST['username'];
$info['user_password'] = $_POST['password'];
$info['remember'] = true;
if((trim($info['user_login']) == "" || $info['user_password'] == "" || strlen(trim($info['user_login'])) < 3 || strlen($info['user_password']) < 6 )){
echo json_encode(array('loggedin'=>false, 'message'=>__('Error')));
}else{
$user_signon = wp_signon( $info, false );
if ( is_wp_error($user_signon) ){
echo json_encode(array('loggedin'=>false, 'message'=>__('Error')));
} else {
wp_set_current_user($user_signon->ID);
echo json_encode(array('loggedin'=>true, 'message'=>__('Error')));
}
}
die();
}
function ajax_auth_init(){
add_action( 'wp_ajax_nopriv_ajaxlogin', 'ajax_login' );
}
if (!is_user_logged_in()) {
add_action('init', 'ajax_auth_init');
}
Why I can't connect directly to wp-admin?
I logged in my site with custom ajax. Then try to connect my wp-admin, the page redirect me to login.php
My custom ajax function in php;
function ajax_login(){
check_ajax_referer( 'ajax-login-nonce', 'security' );
$info = array();
$info['user_login'] = $_POST['username'];
$info['user_password'] = $_POST['password'];
$info['remember'] = true;
if((trim($info['user_login']) == "" || $info['user_password'] == "" || strlen(trim($info['user_login'])) < 3 || strlen($info['user_password']) < 6 )){
echo json_encode(array('loggedin'=>false, 'message'=>__('Error')));
}else{
$user_signon = wp_signon( $info, false );
if ( is_wp_error($user_signon) ){
echo json_encode(array('loggedin'=>false, 'message'=>__('Error')));
} else {
wp_set_current_user($user_signon->ID);
echo json_encode(array('loggedin'=>true, 'message'=>__('Error')));
}
}
die();
}
function ajax_auth_init(){
add_action( 'wp_ajax_nopriv_ajaxlogin', 'ajax_login' );
}
if (!is_user_logged_in()) {
add_action('init', 'ajax_auth_init');
}
Why I can't connect directly to wp-admin?
Share Improve this question asked Jun 12, 2017 at 5:27 Abuzer KillibacakAbuzer Killibacak 578 bronze badges 2- Reload the page on ajax success after use is logged in. Write reload script below wp_set_current_user($user_signon->ID); this line in code. – Jiten Gaikwad Commented Jun 12, 2017 at 6:13
- I'm already using that. But I got it. See my answer. Thank you. – Abuzer Killibacak Commented Jun 12, 2017 at 6:14
1 Answer
Reset to default 2I got it. Just I need to change
$user_signon = wp_signon( $info, false );
with
$user_signon = wp_signon( $info, is_ssl() ? true : false );