Whenever a WordPress theme or plugin handles user-submitted data, you should validate that data before saving it to the database. That means validating required fields, formats, ranges, and similar rules, even if you already validated the same fields in JavaScript on the front end. The library introduced here is Valitron, one of the most widely used server-side validation libraries in PHP. It is simple, lightweight, elegant, and does not depend on any other framework.

Installing Valitron
Like many modern PHP libraries, Valitron can be installed and upgraded directly with Composer:
php composer.phar require vlucas/valitron
Validating Data with Valitron
To validate data, instantiate the Validator class with the array you want to check. Then add rules. If validation fails, you can retrieve the error messages with $v->errors().
$v = new ValitronValidator( array('name' => 'Validate me and I will pass.') );
$v->rule('required', 'name');
if ($v->validate()) {
echo "Validation passed.";
} else {
print_r($v->errors());
}
You can also validate $_POST directly. In the following example, the name and email fields are required, and email must contain a valid email address.
$v = new ValitronValidator($_POST);
$v->rule('required', ['name', 'email']);
$v->rule('email', 'email');
if ($v->validate()) {
echo "Validation passed only if the user submitted both a name and an email address.";
} else {
print_r($v->errors());
}
Built-In Validation Rules
Valitron comes with a rich set of built-in validation rules, which are usually enough for most WordPress development tasks. If the defaults are not enough, you can also define your own rules.
required— required fieldequals— must match another fielddifferent— must be different from another fieldaccepted— checkbox or radio field must be acceptednumeric— must be numericinteger— must be an integerarray— must be an arraylength— must have a fixed lengthlengthBetween— must be between two lengthslengthMin— minimum lengthlengthMax— maximum lengthmin— minimum numeric valuemax— maximum numeric valuein— must be one of a given set of valuesnotIn— must not be one of a given set of valuesip— must be a valid IP addressemail— must be a valid email addressurl— must be a valid URLurlActive— must be a reachable URLalpha— alphabetic characters onlyalphaNum— letters and numbers onlyslug— a valid URL slugregex— must match a custom regular expressiondate— must be a valid datedateFormat— must match a specific date formatdateBefore— must be before a given datedateAfter— must be after a given datecontains— must contain a given substringcreditCard— must be a 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 practical note: if you need highly accurate min and max validation for floating-point numbers, it is a good idea to install the PHP BCMath extension. Valitron will use it automatically when it is available.
Customizing Error Messages
If the default validation messages are not suitable, you can point Valitron to your own language directory and load a custom language file:
use ValitronValidator as V;
V::langDir(__DIR__.'/validator_lang');
V::lang('zh-cn');
Valitron makes it much easier to validate form submissions consistently. Once validation becomes easier, developers are far less likely to skip it. Better validation means more reliable applications, fewer bugs, and stronger security.
