Whenever a WordPress theme or plugin needs to handle user-submitted data, we should validate that data on the server before saving it to the database. Required fields, format checks, and value constraints all matter, even if we already performed client-side validation with JavaScript.
One of the most popular PHP validation libraries for this job is Valitron. It is small, elegant, standalone, and does not depend on other heavy packages. The API is simple enough that it fits very comfortably into WordPress development.
Install the Valitron validation library
Like most popular PHP libraries, Valitron can be installed and managed directly through Composer.

php composer.phar require vlucas/valitron
Validate data with Valitron
Valitron validates whatever data array we pass into the validator. In the following example, an array is passed to the Validator class and then a required rule is added for the name field. If validation fails, the error messages can be read through $v->errors().
$v = new ValitronValidator( array('name' => '来呀,来验证我呀,肯定会通过验证的。') );
$v->rule('required', 'name');
if($v->validate()) {
echo "早告诉你了,肯定会验证通过的。";
} else {
// Print errors
print_r($v->errors());
}
In a real form handler, it is common to pass $_POST directly into the validator. Multiple fields can share the same rule, which keeps the code compact.
$v = new ValitronValidator($_POST);
$v->rule('required', ['name', 'email']);
$v->rule('email', 'email');
if($v->validate()) {
echo "Whether validation passes depends on whether the user entered a name and email.";
} else {
// Print errors
print_r($v->errors());
}
Built-in validation rules
Valitron includes a large set of built-in rules that already cover most of what a WordPress application needs. If they are still not enough, custom rules can be added later.
required– required fieldequals– must equal another fielddifferent– must differ from another fieldaccepted– checkbox or radio must be acceptednumeric– must be numericinteger– must be an integerarray– must be an arraylength– exact lengthlengthBetween– length between two valueslengthMin– minimum lengthlengthMax– maximum lengthmin– minimum numeric valuemax– maximum numeric valuein– must be in a given arraynotIn– must not be in a given arrayip– valid IP addressemail– valid email addressurl– valid URLurlActive– URL that can actually be reachedalpha– letters onlyalphaNum– letters and numbers onlyslug– URL-safe slug stringregex– match a regular expressiondate– valid datedateFormat– date in a specific formatdateBefore– valid date before another datedateAfter– valid date after another datecontains– must contain a specific stringcreditCard– valid credit card numberinstanceOf– must be an instance of a given PHP classoptional– field may be omitted, but if present it must pass validation
One useful note from the original article: if you need accurate min/max validation for floating-point values, installing the PHP BCMath extension is recommended because Valitron uses it when available.
Customize validation failure messages
If Valitron’s default validation messages do not match your project, you can point the library at your own language directory and load a custom language set.
use ValitronValidator as V;
V::langDir(__DIR__.'/validator_lang');
V::lang('zh-cn');
Using Valitron makes it much less tempting to skip validation entirely. Better validation means fewer bugs, more stable application behavior, and safer handling of user-submitted content.
