Здравствуйте
Прописал вот такую функцию в шаблон
function ajax_register_new_user() {
$user_login = $_POST['user_login'];
$user_email = $_POST['user_email'];
$sanitized_user_login = sanitize_user( $user_login );
/**
* Filter the email address of a user being registered.
*
* @since 2.1.0
*
* @param string $user_email The email address of the new user.
*/
$user_email = apply_filters( 'user_registration_email', $user_email );
// Check the username
if ( $sanitized_user_login == '' ) {
echo json_encode(array('loggedin'=>false, 'message'=>__("<strong>ERROR</strong>: Please enter a username")));
} elseif ( ! validate_username( $user_login ) ) {
echo json_encode(array('loggedin'=>false, 'message'=>__("<strong>ERROR</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.")));
$sanitized_user_login = '';
} elseif ( username_exists( $sanitized_user_login ) ) {
echo json_encode(array('loggedin'=>false, 'message'=>__("<strong>ERROR</strong>: This username is already registered. Please choose another one.")));
}
// Check the e-mail address
if ( $user_email == '' ) {
echo json_encode(array('loggedin'=>false, 'message'=>__('<strong>ERROR</strong>: Please type your e-mail address.')));
} elseif ( ! is_email( $user_email ) ) {
echo json_encode(array('loggedin'=>false, 'message'=>__('<strong>ERROR</strong>: The email address isn’t correct.')));
$user_email = '';
} elseif ( email_exists( $user_email ) ) {
echo json_encode(array('loggedin'=>false, 'message'=>__('<strong>ERROR</strong>: This email is already registered, please choose another one.')));
}
$user_pass = wp_generate_password( 12, false );
$user_id = wp_create_user_( $sanitized_user_login, $user_pass, $user_email );
if ( ! $user_id || is_wp_error( $user_id ) ) {
echo json_encode(array('loggedin'=>false, 'message'=>__( '<strong>ERROR</strong>: Couldn’t register you… please contact the <a href="mailto:%s">webmaster</a> !')));
}
update_user_option( $user_id, 'default_password_nag', true, true ); //Set up the Password change nag.
wp_new_user_notification( $user_id, $user_pass );
if (is_numeric($user_id)) {
echo json_encode(array('loggedin'=>false, 'message'=>__("Проверьте почту")));
}
}
и js к ней
jQuery(document).ready(function($) {
$('.form_wrapper #slick-reg').on('submit', function(e){
$('.form_wrapper .status_register').show().text(ajax_register_object.loadingmessage);
$.ajax({
type: 'POST',
dataType: 'json',
url: ajax_register_object.ajaxurl,
data: {
'action': 'ajaxreg', //calls wp_ajax_nopriv_ajaxlogin
'user_login': $('.form_wrapper #user_login').val(),
'user_email': $('.form_wrapper #user_email').val()
},
success: function(data){
$('.form_wrapper .status_register').text(data.message);
if (data.loggedin == true){
document.location.href = ajax_login_object.redirecturl;
}
}
});
e.preventDefault();
});
});
Но все равно при успешной регистрации обновляет страницу, а при неуспешной - редиректит на wp-login.
Как обойти?
Кто подскажет?