It is possible to add custom form validation logic to any form created by the Mailchimp for WordPress plugin using a filter hook.
The following snippet will throw an error if a field named some-field
does not have the value expected-value
.
/**
* Performs additional form validation on Mailchimp for WP forms
*
* @return array An array of error codes.
*/
function myprefix_validate_mc4wp_form($errors) {
// perform validation here
if( $_POST['some-field'] !== 'expected-value' ) {
$errors[] = 'incorrect_value';
}
return $errors;
}
add_filter('mc4wp_form_errors', 'myprefix_validate_mc4wp_form');
To show a custom message for the incorrect_value
error code returned above, you will have to register a custom form message as well.
/**
* Registers an additional Mailchimp for WP error message to match our error code from above
*
* @return array Messages for the various error codes
*/
function myprefix_add_mc4wp_error_message($messages) {
$messages['incorrect_value'] = 'Please enter the correct value.';
return $messages;
}
add_filter('mc4wp_form_messages', 'myprefix_add_mc4wp_error_message');