ajax - Check the username before registering in Wordpress -
i created script checking username before registering, not work. returns mistake. script in functions.php:
function check_user_login_init(){ /* Подключаем скрипт для проверки */ wp_register_script('ajax-login-script', get_template_directory_uri() . '/js/ajax-login-script.js', array('jquery') ); wp_enqueue_script('ajax-login-script'); /* Локализуем параметры скрипта */ wp_localize_script( 'ajax-login-script', 'ajax_check_login_object', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'loadingmessage' => __('verified data, wait second...') )); // Разрешаем запускать функцию check_user_login() пользователям без привелегий add_action( 'wp_ajax_nopriv_check_user_login', 'check_user_login' ); } if (!is_user_logged_in()) { add_action('init', 'check_user_login_init'); } function check_user_login(){ // Первым делом проверяем параметр безопасности check_ajax_referer( 'ajax-login-nonce', 'security' ); // Получаем данные из поля #user_login формы и проверяем их $username = $_request['username']; $sanitized_user_name = sanitize_user( $username, 1 ); if ( username_exists( $sanitized_user_name )) { echo json_encode(array('avaliblename'=>false, 'message'=>__('choose name!'))); } else { echo json_encode(array('avaliblename'=>false, 'message'=>__('ok!'))); } die(); exit; }
and here js:
jquery(document).ready(function($) { $('.register_box #registration #user_login').on('keyup', function(e){ $.ajax({ beforesend: function(){ $('.register_box .status_registration').show().text(ajax_login_object.loadingmessage); }, type: 'post', datatype: 'json', url: ajax_check_login_object.ajaxurl, cache: false, data: { 'action': 'check_user_login', //calls wp_ajax_nopriv_checkuserlogin 'username': $('.register_box #user_login').val() }, datafilter: function(){ $('.register_box .status_registration').html('data processed...'); }, error: function(){ $('.register_box .status_registration').html('something wrong...'); }, success: function(data){ $('.register_box .status_registration').html(data.message); } }); e.preventdefault(); }); });
when try inputting user name in google chrome returns error:
uncaught typeerror: cannot read property 'message' of null
could this?
this javascript error due data.message
not being set. check in network tab of developer toolbar check what's coming server.
side-note: wouldn't hurt enable debugging on server-side idea logs of what's going on. here's how in wp-config.php
:
define('wp_debug', true); define('wp_debug_display', false); // turn forced display off define('wp_debug_log', true); // turn logging wp-content/debug.log on
and executing following line anywhere push wp-content/debug.log
(which can monitor):
error_log('started processing array:'); error_log(print_r($array, true));
finally, rid of die();
, keep exit;
statement.
Comments
Post a Comment