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

    //$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 );

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.

1 Answer 1


Because there is context lacking I'm not able to fix all your problems. One of the mistakes I see is that in your code this line:

if (email_exists($email) === false) {
    $errors[] = 'E-mail address is already in use.';

Must be:

if(email_exists($email)){ etc.

Because you check whether the email_exists function returns an ID when it exists (translates in true) or does not exist, which will give false. Could you give more context (e.g. the form in HTML) for the rest of your question?

EDIT: you make the same mistake with username_exists($username) === false which should just be if(username_exists($username)) instead. Same logic applies.

EDIT EDIT: oh, actually you do that mistake more. Also with in_array( $username, $invalid_usernames ) === false must be: in_array( $username, $invalid_usernames ) Now you are checking the exact opposite of what you want to check I suppose.

  • I am still getting this two error (2) ["Username is invalid.", "E-mail address is invalid."]
    – Toheeb
    Sep 18, 2018 at 11:37
  • Yes, and I actually also think to know why that is the case. You sanitize your inputs with sanitize_text_field. However, this is 'to strict' for e-mails. Instead, for the e-mail address sanitation use sanitize_email instead. Here is a link which explains it better: codex.wordpress.org/Function_Reference/sanitize_email
    – Breus
    Sep 18, 2018 at 11:43
  • I just did but still getting invalid email response
    – Toheeb
    Sep 18, 2018 at 11:49
  • the one that borders me most is username_exists( $username ) I used an existing username but it didn't through an error after the adjustment you asked me to do.
    – Toheeb
    Sep 18, 2018 at 11:52
  • Could you print the sanitized variables and not sanitized POST fields here? Might be due to sanitation. Also, instead of doing $errors[] = 'error message' try using something like array_push instead. secure.php.net/manual/en/function.array-push.php .
    – Breus
    Sep 18, 2018 at 11:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.