Here is my registration function
$firstname = sanitize_text_field( $_POST['firstname'] );
$lastname = sanitize_text_field( $_POST['lastname'] );
$username = sanitize_text_field( $_POST['reg-username'] );
$user_pass = sanitize_text_field( $_POST['reg-password'] );
$pass_confirm = sanitize_text_field( $_POST['confirm_password'] );
$gender = sanitize_text_field( $_POST['gender'] );
$email = sanitize_text_field( $_POST['email'] );
//Add usernames we don't want used
$invalid_usernames = array( 'admin' );
$errors = array();
//Do username validation
$username = sanitize_user( $username );
if ( empty( $username ) || validate_username( $username ) === false || in_array( $username, $invalid_usernames ) === false ) {
$errors[]= 'Username is invalid.';
}
if ( username_exists( $username ) === false) {
$errors[]= 'Username already exists.';
}
//Do e-mail address validation
if ( !is_email( $email ) ) {
$errors[] = 'E-mail address is invalid.';
}
if (email_exists($email) === false) {
$errors[] = 'E-mail address is already in use.';
}
if ($user_pass != $pass_confirm) {
$errors[] = 'Password combination incorrect.';
}
$genders = array('male','female',true);
if ( in_array( $gender,$genders ) === false){
$errors[] = 'Invalid gender';
}
//Everything has been validated, proceed with creating the user
//Create the user
if(!empty($errors)):
wp_send_json_error($errors);
endif;
//$user_pass = wp_generate_password();
$user = array(
'user_login' => $username,
'user_pass' => $user_pass,
'first_name' => $firstname,
'last_name' => $lastname,
'user_email' => $email,
'gender' => $gender
);
$user_id = wp_insert_user( $user );
/*Send e-mail to admin and new user -
You could create your own e-mail instead of using this function*/
wp_new_user_notification( $user_id, $user_pass );
wp_send_json_success($user_id);
I keep getting this error each time.
0:"Username is invalid."
1:"Username already exists."
2:"E-mail address is invalid."
3:"E-mail address is already in use."
4:"Invalid gender"
even when all inputs are correct
The password confirmation function is not working even if password combination is incorrect.