Add custom form validation

It is quite easy to add custom form validation to any form created by MailChimp for WordPress using one of the many filters and action hooks the plugin provides.

The following snippet will throw an error if a field doesn't match its 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' );
 
/**
* Registers an additional MailChimp for WP error message
*
* @return array Array of messages
*/
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' );

Was this article helpful? ·