I am trying to create a registration form with ajax on wordpress. It works without problems. Member can register. However, for example, when I set the password as "123", it gives an error when trying to login with the same membership. Password incorrect error. Where do you think I am doing wrong? I'm not an expert on this. Thanks.
theme.js ;
$('.register-form').on('submit', function(e){
e.preventDefault();
let form = $(this),
submit_button = form.find('button[type="submit"]');
$.ajax({
type: 'POST',
url: yaygin_object.ajax_url,
data: form.serialize() + '&action=yaygin_do_register_form',
dataType: 'json',
beforeSend: function() {
submit_button.attr('disabled', true);
form.find('.alert').remove();
},
success: function (response) {
if(response.success) {
form.prepend('<div class="alert alert-success" role="alert">' + response.data.message + '</div>');
setTimeout( function() {
window.location.href = yaygin_object.home_url;
}, 1000 );
} else {
form.prepend('<div class="alert alert-danger" role="alert">' + response.data.message + '</div>');
}
},
complete: function() {
submit_button.attr('disabled', false);
}
});
});
register.php
<form class="register-form ortala" method="post">
<input type="name" id="name" class="form-control form-control-sm mb-2" name="name" placeholder="<?php esc_attr_e('User Name', 'yaygin'); ?>" required>
<input type="email" id="email" class="form-control form-control-sm mb-2" name="email" placeholder="<?php esc_attr_e('E-Mail', 'yaygin'); ?>" required>
<input type="password" id="password" class="form-control form-control-sm mb-2" name="password" placeholder="<?php esc_attr_e('Password', 'yaygin'); ?>" required>
<button type="submit" class="btn btn-dark btn-sm"><i class="fa fa-user-plus"></i> <?php esc_html_e('Sign up', 'yaygin'); ?></button>
<?php wp_nonce_field('register_action', 'register_field'); ?>
</form>
ajax-functions.php
function yaygin_do_register_form() {
check_ajax_referer('register_action', 'register_field');
$name = sanitize_text_field( $_POST['name'] );
$email = sanitize_email( $_POST['email'] );
$password = sanitize_text_field( $_POST['password'] );
$creds = array(
'user_login' => $name,
'user_password' => $password,
'user_email' => $email,
);
$user = wp_insert_user( $creds, is_ssl() );
if (is_wp_error( $user )) {
wp_send_json_error( array('message' => $user->get_error_message() ) );
}
wp_send_json_success( array('message' => esc_html__('Registration Successful. Please wait...', 'yaygin') ) );}add_action('wp_ajax_nopriv_yaygin_do_register_form', 'yaygin_do_register_form');