0

(1st post)

I have built a custom registration form for my Wordpress site and even though it has complete functionality, I am getting validation errors for both username_exists and email_exists.

This is what happens:

If I fill out the registration form with any mistakes, it doesn't register and it produces the correct errors.

If I fill out the registration form with no mistakes (AND a unique username and email), it registers correctly and inserts the userdata. But it still produces the username_exists and email_exists errors.

I've been wrestling with this for weeks now and haven't been able to come up with a solution. I'll be glad to provide the code if needed.

Thank you!

(main plugin code - sorry if not formatted right. Still getting used to the forum)

function slyd_register() {
    wp_enqueue_style('slyd-register', plugins_url('/css/sr-styles.css', __FILE__));
    wp_enqueue_script( 'slyd_register', plugins_url( '/js/sr-scripts.js', __FILE__ ));
}
add_action('wp_enqueue_scripts','slyd_register');


/////////////////
// PLUGIN CORE //
/////////////////

function cr(&$fields, &$errors) {
  // Check args and replace if necessary
  if (!is_array($fields))     $fields = array();
  if (!is_wp_error($errors))  $errors = new WP_Error;

  // Check for form submit
  if (isset($_POST['reg_submit'])) {
    // Get fields from submitted form
    $fields = cr_get_fields();
    // Validate fields and produce errors
    if (cr_validate($fields, $errors)) {
      // If successful, register user
      wp_insert_user($fields);

            echo '';
            echo 'Registration complete. Please Login';
            echo '';
      // Clear field data
      $fields = array(); 
        }
  }

  // Santitize fields
  cr_sanitize($fields);
  // Generate form
  cr_display_form($fields, $errors);
}

function cr_sanitize(&$fields) {
    $fields['user_role']    =  isset($fields['user_role'])  ? sanitize_text_field($fields['user_role']) : '';
  $fields['user_login']   =  isset($fields['user_login'])  ? sanitize_user($fields['user_login']) : '';
  $fields['user_email']   =  isset($fields['user_email'])  ? sanitize_email($fields['user_email']) : '';
    $fields['user_pass']    =  isset($fields['user_pass'])   ? esc_attr($fields['user_pass']) : '';
    $fields['user_pass2']   =  isset($fields['user_pass2'])   ? esc_attr($fields['user_pass2']) : '';
  $fields['first_name']   =  isset($fields['first_name'])  ? sanitize_text_field($fields['first_name']) : '';
  $fields['last_name']    =  isset($fields['last_name'])  ? sanitize_text_field($fields['last_name']) : '';
  $fields['nickname']     =  isset($fields['nickname'])  ? sanitize_text_field($fields['nickname']) : '';
}


function cr_display_form($fields = array(), $errors = null) {
  // Check for wp error obj and see if it has any errors  
  if (is_wp_error($errors) && count($errors->get_error_messages()) > 0) {
    // Display errors
    foreach ($errors->get_error_messages() as $key=>$val) {
            echo '';
            echo '' . $val . '';
            echo '';
    }
  }

        global $wp_roles;

        if (!is_user_logged_in()) {
?>
     (MAIN FORM HERE)

  $_POST['user_role'],
    'user_login'    =>  $_POST['user_login'],
    'user_email'    =>  $_POST['user_email'],
    'user_pass'     =>  $_POST['user_pass'],
        'user_pass2'    =>  $_POST['user_pass2'],
    'first_name'    =>  $_POST['first_name'],
    'last_name'     =>  $_POST['last_name'],
    'nickname'      =>  $_POST['nickname']
  );
}


function cr_validate(&$fields, &$errors) {
  // Make sure there is a proper wp error obj
  // If not, make one
  if (!is_wp_error($errors))  $errors = new WP_Error;

//role
        if ( empty( $fields['user_role'] ) || ! empty( $fields['user_role'] ) && trim( $fields['user_role'] ) == '' ) {
            $errors->add('role','ERROR: You must include a plan');
        }
//make sure fields are not empty
        $fieldVal = array($fields['user_email'], $fields['first_name'], $fields['last_name'], $fields['nickname']);
        if (in_array('',$fieldVal)) {
            $errors->add('field', 'Missing Entry: All fields are required');
        }
//username
        if (strlen($fields['user_login']) add('username_length', 'Username too short. At least 4 characters are required');
        }
        if (username_exists($fields['user_login'])) {
        $errors->add('user_name', 'The username is already in use');
        }
        if (!validate_username($fields['user_login'])) {
            $errors->add('username_invalid', 'Sorry, the username you entered is not valid');
        }
//email 
        if (!is_email($fields['user_email'])) {
            $errors->add('email_invalid', 'This is not a valid email address');
        }
        if (email_exists($fields['user_email'])) {
            $errors->add('email', 'This email address has already been used');
        }
//password
        if (strlen($fields['user_pass']) add('password', 'Password must be 8 or more characters');
        }
        if (isset($fields['user_pass']) && $fields['user_pass'] != $fields['user_pass2'] ) {
            $errors->add('password_reset_mismatch', 'The passwords do not match');
        }
// If errors were produced, fail
        if (count($errors->get_error_messages()) > 0) {
            return false;
        }

// Else, success!
  return true;
}


///////////////
// SHORTCODE //
///////////////

// The callback function for the [cr] shortcode
function reg_shortcode() {
  $fields = array();
  $errors = new WP_Error();
  // Buffer output
  ob_start();
  // Custom registration, go!
  cr($fields, $errors);
  // Return buffer
  return ob_get_clean();
}
add_shortcode('ss_registration_form', 'reg_shortcode');
  • Probably your logic calls those functions even after inserting the user? "I'll be glad to provide the code if needed" that would be helpful. – Samuel Elh Sep 12 '17 at 17:40
  • I've added the code above. – TechDragon Sep 12 '17 at 18:21
0

[Resolved] - It turns out the issue was a conflict with the Yoast SEO plugin. Yoast was re-sending the headers after the user was registered. So the errors were re-checked and the username_exists / email_exists showed up as duplicates. I was able to get around this by disabling Yoast just for the registration page with a code hack in the functions file.

If you run into this issue, here's the code:

add_action('template_redirect','remove_wpseo');

function remove_wpseo(){
    if (is_page(5526)) {
        global $wpseo_front;
            if(defined($wpseo_front)){
                remove_action('wp_head',array($wpseo_front,'head'),1);
            }
            else {
              $wp_thing = WPSEO_Frontend::get_instance();
              remove_action('wp_head',array($wp_thing,'head'),1);
            }
    }
}

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.