0

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
}
}


}
1
  • Then you're going to need to send an AJAX request to validate the username while still on the page Commented Mar 22, 2017 at 1:07

1 Answer 1

2

It sounds like you're going to need to make an AJAX call on your username input field and bind it to the onkeyup event. Then it would call to a PHP file that looked something like this, with the results going in a div that would appear out next to the input with a success/fail message. You can pass the entered text as url variable named "user".

You would also need to keep the code that you already have because it will have to be re-validated when the user actually submits and if it fails, they'll have to go back.

<?php
      include("Path to where the username_exists function is defined");
      $user = sqlescape($_GET["user"]);

      if ( username_exists($user)){
          echo 'That username has already been claimed.';
      } else {
          echo 'That username is available.';
      }
?>

The AJAX call will go on the actual form (below wherever your input is) and look something like this:

<script type="text/javascript">
    $( "#input_id_here" ).keyup(function() {
     var requestURL = "Full URL where above PHP file is located?user=" + $(this).val();

    $.ajax({
        url: requestURL,
        context: this,
        type: 'GET',
        success: function(res) 
        { 
            $('#input_error_div_id_here').html(res);    
        }
     });
 });
</script>

You could also add/remove classes to your input or error div based on the value returned in the AJAX call. That Javascript would be added within the success part.

Sign up to request clarification or add additional context in comments.

1 Comment

I've never had to use AJAX, especially with wordpress. Do you know how this would happen?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.