Error messages cannot be customized for each field type
-
Can I override error messages for required fields that are left blank via a hook?
## What I tried
I tried to change the error message for an empty required checkbox in my form to something different from the text field.
For example, something like “Please select at least one option.”I wrote the following code in functions.php, but no change occurred.
add_filter('wpcf7_validate_checkbox*', 'custom_checkbox_validation', 99, 2); function custom_checkbox_validation($result, $tag) { if ($tag->name == 'my-checkbox') { $value = isset($_POST[$tag->name]) ? $_POST[$tag->name] : array(); if (empty($value)) { $result->invalidate($tag, "Please select at least one option."); } } return $result; }When I commented out the following line in this plugin as a test, only the checkbox error message displayed as intended.
(Of course, all other error messages are no longer displayed.)I suspect this line is overriding
wpcf7_validate_checkbox*.Thanks for taking the time to read this!
Contact Form 7 : v6.1.1
WordPress : v6.8.2
The topic ‘Error messages cannot be customized for each field type’ is closed to new replies.