"Confirm Email" Field for Contact Form 7
Makes visitors type their email twice and shows an error when the two addresses do not match, so you do not lose replies to typos.
Opens WPIDE on
Code
$wpide_cf7_email_field = 'your-email';
$wpide_cf7_confirm_field = 'your-email-confirm';
$wpide_cf7_confirm_check = function ( $result, $tag ) use ( $wpide_cf7_email_field, $wpide_cf7_confirm_field ) {
if ( $tag->name !== $wpide_cf7_confirm_field ) {
return $result;
}
$email = isset( $_POST[ $wpide_cf7_email_field ] ) ? strtolower( trim( (string) wp_unslash( $_POST[ $wpide_cf7_email_field ] ) ) ) : '';
$confirm = isset( $_POST[ $wpide_cf7_confirm_field ] ) ? strtolower( trim( (string) wp_unslash( $_POST[ $wpide_cf7_confirm_field ] ) ) ) : '';
if ( $email !== $confirm ) {
$result->invalidate( $tag, 'The email addresses do not match.' );
}
return $result;
};
add_filter( 'wpcf7_validate_email', $wpide_cf7_confirm_check, 20, 2 );
add_filter( 'wpcf7_validate_email*', $wpide_cf7_confirm_check, 20, 2 );
Notes
Add a second email field named your-email-confirm to the form, for example [email* your-email-confirm], or change the names at the top.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.