问题
Does anyone know how to validate wordpress username and email with jquery validation plugin?
I'm trying to check if username and email exists with validation's remote method.
And I notice wordpress has functions like username_exists and email_exists, is there a quick way to do the job?
Plz help~
回答1:
You can use something like this on client side (the ajax URL does not have to be hardcoded, check note here: http://codex.wordpress.org/AJAX_in_Plugins#Ajax_on_the_Viewer-Facing_Side):
"user_email":{
remote: {
url: 'YOURSITE.COM/wp-admin/admin-ajax.php',
type: "post",
data: {
'user_email': function() {
return $( "#user_email" ).val();
},
'action': 'check_user_email'
}
}
}
And this in functions.php:
add_action( 'wp_ajax_nopriv_check_user_email', 'check_user_email_callback' );
function check_user_email_callback() {
global $wpdb; // this is how you get access to the database
if(email_exists($_POST['user_email'])){
echo json_encode('error.');
}
else{
echo json_encode('true');
}
die();
}
来源:https://stackoverflow.com/questions/15058899/ajax-remote-validate-wordpress-username-and-email-with-jquery-validation-plugin