Ajax remote validate wordpress username and email

2019-04-13 17:43发布

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条回答
神经病院院长
2楼-- · 2019-04-13 18:24

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();
}
查看更多
登录 后发表回答