I have an issue with my wordpress site. I created a custom HTML form with custom PHP in the functions file so that when the form is submitted it creates a user and emails me. It works great but there's one thing I'd like to accomplish: When someone creates a username, I'd like it to put an error tag right by the box that the username exists if it's already in the database. Below is our current code that handles this operation, and it currently goes to an index page with error message and redirects back to the form in 5 seconds. I would prefer to handle the validation before the form is submitted though.
if ( username_exists($user)){
echo'User already exists. Go back and try again';
die();
}
if ( !username_exists( $user ) && !email_exists( $email ) ) {
$user_id = wp_create_user( $user, $pass, $email );
if( !is_wp_error($user_id) ) {
//user has been created
$user = new WP_User( $user_id );
$user->set_role( 'subscriber' );
wp_mail( $to, $email_subject, $message, $headers);
//Redirect
wp_redirect( 'http://www.dev.smithhonig.com/thank-you' );
exit;
} else {
//$user_id is a WP_Error object. Manage the error
}
}
}