I’ve created a custom ajax login as per this tutorial but I’m having an issue. I’m using this jquery code to send the ajax request:
function ajaxLogin(form) {
$.ajax({
type: 'post',
dataType: 'html',
url: loginScript.ajaxurl,
data: "action=cb_register_login_submit&" + $(form).serialize(),
global: false,
success: function( msg ){
//location.reload( true ); // This could be nicer
handleLoginReturn(msg);
},
error: function(er) {
handleErrorReturn(er);
}
});
}
function handleLoginReturn(msg) {
console.log(msg);
}
and the following PHP function to handle the login process:
<?php
function cb_register_login_submit() {
$creds = array();
$creds['user_login'] = $_POST['username'];
$creds['user_password'] = $_POST['pwd'];
if ( isset( $_POST['remember'] ) ) {
$creds['remember'] = true;
}
$user = wp_signon( $creds, false );
if ( is_wp_error( $user ) ) {
echo 'error';
} else {
echo 'success';
}
die(0);
}
?>
So the problem is that when I enter the wrong login info, I do in fact get the string “error” returned through the ajax success callback but when I put in the correct log info, an entire page worth of html (which I think is the wordpress dashboard code) is returned through the success callback instead of the string “success”. In fact the string “success” doesn’t seem to be returned anywhere in the success callback.
Any idea what’s wrong here? I am assuming that maybe the wp_signon function is returning data on success that I’m unaware of. Any help would be much appreciated!
I found the solution:
I was using the “Login Logout Redirect” plugin which seems to have been sending the redirect page html upon a successful ajax call. I deactivated the plugin and everything’s working splendidly!