The customize_save_validation_before WordPress PHP action fires before save validation happens, allowing plugins to add just-in-time filters for catching settings registered after customize_register
.
Usage
add_action('customize_save_validation_before', 'my_custom_function'); function my_custom_function($manager) { // your custom code here }
Parameters
$manager
(WP_Customize_Manager) – The instance of the WP_Customize_Manager.
More information
See WordPress Developer Resources: customize_save_validation_before
Examples
Add a custom validation for a setting
Add a validation for the ‘my_custom_setting’ setting, checking if the value is a positive number.
add_action('customize_save_validation_before', 'add_my_custom_setting_validation'); function add_my_custom_setting_validation($manager) { $manager->get_setting('my_custom_setting')->transport = 'postMessage'; add_filter("customize_validate_{$manager->get_setting('my_custom_setting')->id}", 'validate_my_custom_setting', 10, 2); } function validate_my_custom_setting($validity, $value) { if (!is_numeric($value) || $value < 0) { $validity->add('not_positive_number', 'Value should be a positive number.'); } return $validity; }
Add a custom validation for a text input
Add a custom validation for a text input, ensuring it is not empty.
add_action('customize_save_validation_before', 'add_text_input_validation'); function add_text_input_validation($manager) { add_filter("customize_validate_{$manager->get_setting('text_input')->id}", 'validate_text_input', 10, 2); } function validate_text_input($validity, $value) { if (empty($value)) { $validity->add('empty_input', 'Text input should not be empty.'); } return $validity; }
Add a custom validation for an email input
Add a custom validation for an email input, ensuring it is a valid email.
add_action('customize_save_validation_before', 'add_email_input_validation'); function add_email_input_validation($manager) { add_filter("customize_validate_{$manager->get_setting('email_input')->id}", 'validate_email_input', 10, 2); } function validate_email_input($validity, $value) { if (!filter_var($value, FILTER_VALIDATE_EMAIL)) { $validity->add('invalid_email', 'Please enter a valid email address.'); } return $validity; }
Add a custom validation for a URL input
Add a custom validation for a URL input, ensuring it is a valid URL.
add_action('customize_save_validation_before', 'add_url_input_validation'); function add_url_input_validation($manager) { add_filter("customize_validate_{$manager->get_setting('url_input')->id}", 'validate_url_input', 10, 2); } function validate_url_input($validity, $value) { if (!filter_var($value, FILTER_VALIDATE_URL)) { $validity->add('invalid_url', 'Please enter a valid URL.'); } return $validity; }