0

My problem is that I can't verify if some user insert an existing email into AJAX registration form. However my form is working fine, it's registering users and it's giving all other errors, like "invalid email" or "empty fields". The only thing it doesn't disaply is the error for the existing emails.

function ajax_register(){

        // First check the nonce, if it fails the function will break
        check_ajax_referer( 'ajax-register-nonce', 'security' );

        $user_login = $_POST['user_login'];
        $sanitized_user_login = sanitize_user( $user_login );
        $user_email = $_POST['user_email'];
        $user_pass = wp_generate_password( 12, false);
        $user_tp = $_POST['user_tp'];
        if(empty($user_tp)) $capa = 'subscriber';
        else $capa = $user_tp;

        //Adding errors

        $newerrors = my_errors($user_email);

        //CREATE USERS

        $user_id = wp_create_user( $sanitized_user_login, $user_pass, $user_email, $capa );

        if (is_wp_error($user_id)){

            //VERIFYNG WITH DEFAULT ERRORS

        } elseif (is_wp_error($newerrors)){

            //VERIFYING WITH MY CUSTOM ERRORS

                echo json_encode(array('loggedin'=>false, 'message'=>__($newerrors->get_error_message())));

            } else {

            //REGISTER USERS
        }
        die();
    }

    //MY CUSTOM ERROR FUNCTION

    function my_errors($user_email) {
        $errors = new WP_Error();
        $user_email = apply_filters( 'user_registration_email', $user_email );
        if ( $user_email == '' ) {
            $errors->add( 'empty_email', __( 'Please, insert your email.') );
        } elseif ( ! is_email( $user_email ) ) {
            $errors->add( 'invalid_email', __( 'The email is not valid.') );
            $user_email = '';

        //THIS IS WHERE I CHECK THE EXISTING EMAIL ***

        } elseif ( email_exists( $user_email ) ) {
            $errors->add( 'registered', __( 'This email is already registered.' ) );
        }
        if ($errors->get_error_code())
        return $errors;
    }

*** At this point I verify if the $user_email is existing already. But when I test it the message isn't displayed and the form is stuck.

Is there a need of an add_action? Can you explain me where I'm wrong? Thanks

1

Why are you making work hard for yourself? wp_create_user already checks if the email/login exists, which is also why your code is "failing" - $user_id will already be a WP_Error, so your elseif ( is_wp_error( $newerrors ) ) never fires.

All you need is:

$user_id = wp_create_user( $sanitized_user_login, $user_pass, $user_email, $capa );

if ( is_wp_error( $user_id ) ) {
    wp_send_json( array(
        'loggedin' => false,
        'message'  => $user_id->get_error_message(),
    ) );
}

exit;

Also note I've used the WordPress helper wp_send_json, which is the correct way of sending JSON data back to the client.

  • You made my day. I didn't know about wp_send_json and wp_create_user either. Wow! Thanks again! – huraji May 12 '16 at 11:56
0

You could create your own email_exists function to check it in a different way, I ended up doing this in a plugin as I was not getting consistent results with email_exists for some reason:

function check_email_exists($email) {
    global $wpdb;
    $user_id = $wpdb->get_var($wpdb->prepare("SELECT ID FROM ".$wpdb->users." WHERE user_email = '%s'",$email));
    return $user_id;
}
  • WHY!? email_exists – TheDeadMedic May 12 '16 at 11:42
  • It was ages ago, I can't remember the specific problem I was having but it did solve it at the time. – majick May 12 '16 at 12:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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