The customize_validate_{$this->id} WordPress PHP filter validates a Customize setting value. Plugins should amend the $validity
object via its WP_Error::add() method. The dynamic portion of the hook name, $this->ID
, refers to the setting ID.
Usage
add_filter('customize_validate_{$this->id}', 'your_custom_function', 10, 3); function your_custom_function($validity, $value, $setting) { // your custom code here return $validity; }
Parameters
$validity
(WP_Error): Filtered from true to WP_Error when invalid.$value
(mixed): Value of the setting.$setting
(WP_Customize_Setting): WP_Customize_Setting instance.
More information
See WordPress Developer Resources: customize_validate_{$this->id}
Examples
Validate email input
Validate if the input is a valid email address.
add_filter('customize_validate_email_input', 'validate_email_input', 10, 3); function validate_email_input($validity, $value, $setting) { if (!filter_var($value, FILTER_VALIDATE_EMAIL)) { $validity->add('invalid_email', 'Invalid email address.'); } return $validity; }
Validate phone number input
Validate if the input is a valid phone number.
add_filter('customize_validate_phone_input', 'validate_phone_input', 10, 3); function validate_phone_input($validity, $value, $setting) { if (!preg_match('/^\d{10}$/', $value)) { $validity->add('invalid_phone', 'Invalid phone number. Please enter a 10-digit phone number.'); } return $validity; }
Validate minimum character length
Validate if the input meets the minimum character length requirement.
add_filter('customize_validate_minimum_chars', 'validate_minimum_chars', 10, 3); function validate_minimum_chars($validity, $value, $setting) { if (strlen($value) < 10) { $validity->add('short_input', 'The input should be at least 10 characters long.'); } return $validity; }
Validate maximum character length
Validate if the input does not exceed the maximum character length limit.
add_filter('customize_validate_maximum_chars', 'validate_maximum_chars', 10, 3); function validate_maximum_chars($validity, $value, $setting) { if (strlen($value) > 140) { $validity->add('long_input', 'The input should not exceed 140 characters.'); } return $validity; }
Validate URL input
Validate if the input is a valid URL.
add_filter('customize_validate_url_input', 'validate_url_input', 10, 3); function validate_url_input($validity, $value, $setting) { if (!filter_var($value, FILTER_VALIDATE_URL)) { $validity->add('invalid_url', 'Invalid URL.'); } return $validity; }