Update: Error was a simple typo, disregard this question.
我正在使用jQuery验证插件验证一些表单。当我尝试使用
is_user_logged_in()
函数,我在PHP中得到以下错误:
PHP致命错误:未捕获错误:对未定义函数的调用is\\u user\\u logged\\u in()
我知道这是WordPress加载顺序的问题,但我不确定最好的解决方案是什么。
我的代码如下所示。内部具有验证函数的类,该函数从wp_ajax
挂钩。
// Class containing validation function
class NS_User_System_Password_Settings {
// ...
public function validate_current_password( $current_password = NULL ) {
// Check if logged in
if ( !is_user_loggged_in() ) {
return $this->get_error( \'not_logged_in\' );
}
// Rest of validation...
}
// ...
}
// Action hook for AJAX validation
add_action( \'wp_ajax_ns_account_settings_current_password_validation_callback\', \'ns_account_settings_current_password_validation_callback\' );
add_action( \'wp_ajax_nopriv_ns_account_settings_current_password_validation_callback\', \'ns_account_settings_current_password_validation_callback\' );
function ns_account_settings_current_password_validation_callback() {
$validate_current_password = NS_User_System_Password_Settings()->validate_current_password( $_POST[\'current_password\'] );
if ( is_wp_error( $validate_current_password ) ) {
echo json_encode( $validate_current_password->get_message() );
} else {
echo json_encode( true );
}
wp_die();
}
在jQuery中,我这样调用验证操作:
$(\'#account-settings-password-form\').validate({
submitHandler: function(form) {
$.ajax({
url: ajax_url,
type: \'POST\',
data: $(form).serialize(),
success: function(response) {
// ...
},
complete: function() {
// ...
}
});
},
rules: {
current_password: {
required: true,
minlength: 6,
maxlength: 60,
remote: {
url: ajax_url,
type: \'post\',
data: {
\'action\': \'ns_account_settings_current_password_validation_callback\',
\'current_password\': function() {
return $(\'#account-current-password\').val();
}
}
}
},
}
});