Ajax remote validate wordpress username and email with Jquery validation plugin

蓝咒 提交于 2019-12-21 21:10:59

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!