I'm having some trouble with my custom ajax function. I have this on my console log : "POST MYSITEURL/wp-admin/admin-ajax.php 302 Found". So my functions won't work...
And I really don't understand why.
I've developed a login form for the front end, it works well on my local but not online. Here is what I do :
function ajax_login_init(){
//First I register my scripts
wp_register_script('ajax-login-script', get_template_directory_uri() . '/js/ajax-login-script.js', array('jquery'),'',true );
wp_enqueue_script('ajax-login-script');
//I give them variables
wp_localize_script( 'ajax-login-script', 'ajax_login_object', array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'redirecturl' => '/account'
));
}
function allowAjaxLogin(){
//I allow the function
add_action( 'wp_ajax_nopriv_ajaxlogin', 'ajax_login' );
add_action( 'wp_ajax_priv_ajaxlogin', 'ajax_login' );
}
add_action( 'init','allowAjaxLogin' );
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'] = $_POST['remember'];
$user_signon = wp_signon( $info, false );
if ( is_wp_error($user_signon) ){
echo json_encode(array('loggedin'=>false, 'message'=>__('Wrong username/password')));
} else {
echo json_encode(array('loggedin'=>true, 'message'=>__('Success)));
}
die();
}
As I said, it works well on my local version, but not online, someone can help me?
get_temlate_directory_uri()
tells me you're using it in a theme.