(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');