Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using buddypress which uses its own template form register.php for registration at http://mysite.com/register. I'm looking for a way to show the user whether or not their entered username is available using ajax validation. Here is a snippet from the form.

<?php do_action( 'template_notices' ) ?>
<?php do_action( 'bp_before_account_details_fields' ) ?>
   <div class="register-section" id="basic-details-section">
      <?php /***** Basic Account Details ******/ ?>
  <?php do_action( 'bp_signup_username_errors' ) ?>
  <input type="text" name="signup_username" id="signup_username" value="Username" />
  <?php do_action( 'bp_signup_email_errors' ) ?>
  <input type="text" name="signup_email" id="signup_email" value="Email" />
   </div><!-- #basic-details-section -->
share|improve this question
    
Please let us know if the below answer worked –  AlxVallejo Oct 29 '12 at 23:28
add comment

1 Answer 1

The following untested code should get you started. Basically you add a display target on your form, add some javascript to add the validation when the user leaves the username field, and the server-side code to enqueue the javascript, and register the ajax actions.

Let me know what breaks, as I'm sure this will not run out of the box.

[!--HTML to add to registration form after user_name (replace square brackets w/angled) --]
[div id="validate_user_login_results"][/div]

## contents of validate_login.js
##
jQuery(document).ready(function(){
     jQuery('body.login div#login form#loginform input#user_login').onblur(function(){
          jQuery.post( ajaxurl
                       , {
                           'action':'validate_user_login',
                           'username': $('body.login div#login form#loginform input#user_login').val()
                         }
                       , function(response){
                            var res = wpAjax.parseAjaxResponse(response, 'ajax-response');
                            jQuery.each( res.responses, function() {
                                $('#validate_user_login_results').load($this.data);
                                $("#validate_user_login_results").fadeIn("fast");
                                ## if $this.data == 'OK' then success action else failure action
                            }
                         }
                     );
     });
});

// add to site plugin or functions.php
//
function wpse64865_add_validate_login_js(){  
  wp_enqueue_script( 'validate_login.js'
                     , get_bloginfo('template_directory') 
                      . "/scripts/validate_login.js" // change this to correctly locate javascript
                      , array( 'jquery','wp-ajax-response' ) ); 
}  
add_action( 'init', 'wpse64865_add_validate_login_js' );

add_action('wp_ajax_validate_user_login', 'wpse64865_ajax_validate_user_login');
add_action('wp_ajax_validate_nopriv_user_login', 'wpse64865_ajax_validate_user_login');


function wpse64865_ajax_validate_user_login() {
    //Handle request then generate response using WP_Ajax_Response
    $username = $_REQUEST['username']

    if ( validate_username( $username )) {
        $response = array( 'what'=>'validate_user_login',
                           'action'=>'validate_user_login_results',
                           'id'=>'1',
                           'data'=>'Not OK' // login exists
                          );
    } else {
         $response = array( 'what'=>'validate_user_login',
                           'action'=>'validate_user_login_results',
                           'id'=>'1',
                           'data'=>'OK' // check failed, login doesn't exist
                          );
    }
    $xmlResponse = new WP_Ajax_Response($response);
    $xmlResponse->send();
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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