Have you ever filled out a form and been asked to confirm your email address by typing it twice? This is a great way to ensure that users submit the correct email. In this quick tutorial, I’ll show you how to add email confirmation validation to Contact Form 7 in WordPress, using a simple code snippet.
add_filter( 'wpcf7_validate_email*', 'custom_email_confirmation_validation_filter', 20, 2 );
function custom_email_confirmation_validation_filter( $result, $tag ) {
if ( 'your-email-confirm' == $tag->name ) {
$your_email = isset( $_POST['your-email'] ) ? trim( $_POST['your-email'] ) : '';
$your_email_confirm = isset( $_POST['your-email-confirm'] ) ? trim( $_POST['your-email-confirm'] ) : '';
if ( $your_email != $your_email_confirm ) {
$result->invalidate( $tag, "Are you sure this is the correct address?" );
}
}
return $result;
}
Breaking It Down
Let’s walk through what’s happening here:
- Hooking Into Contact Form 7’s Validation
The first line of the code adds a filter to Contact Form 7’s email validation process. Specifically, we’re hooking into thewpcf7_validate_email*
filter, which validates email fields marked as required in your form. - Custom Validation Function
The functioncustom_email_confirmation_validation_filter
checks if the user has correctly confirmed their email address by comparing two fields: the original email and the confirmation email. - Email Comparison
Inside this function, we check if the current form field being validated is the email confirmation field (in our case, the field name isyour-email-confirm
). If it is, we retrieve the values from both the original email (your-email
) and the confirmation email (your-email-confirm
) fields. Then we compare them. - Showing an Error Message
If the two emails don’t match, we tell Contact Form 7 to invalidate the field and display a custom error message: “Are you sure this is the correct address?” - Return the Result
No matter what happens (whether the emails match or not), the result is returned so that Contact Form 7 can continue with its validation.
How to Use This in Your Form
- Add the code to your theme’s
functions.php
file or a custom plugin. - Edit your Contact Form 7 form to include both the main email field and a confirmation field. Make sure to name the confirmation field
your-email-confirm
. Here’s a basic example:
[email* your-email placeholder "Enter your email"]
[email* your-email-confirm placeholder "Confirm your email"]
3. When a user submits the form, if the two email addresses do not match, they will see the validation error message.
Customizing the Error Message
Don’t like the default error message? No problem. You can easily change it by editing this part of the code:
$result->invalidate( $tag, "Are you sure this is the correct address?" );
For example, you could make it say something like:
$result->invalidate( $tag, "Oops! The email addresses don’t match. Please try again." );
Why Add Email Confirmation?
Adding an email confirmation field to your forms isn’t just about making users double-check their input—it can also help you ensure that you’re collecting accurate contact information. If a user accidentally enters the wrong email address, you won’t be able to contact them. With this extra validation step, you reduce the chances of losing leads or customers due to a simple typo.
This method is especially useful for forms that require accurate communication, such as newsletter signups, account creation, or anything involving email verification.
That’s it! With just a few lines of code, you can improve the reliability of your form submissions by ensuring that users confirm their email address. If you have any questions or run into issues, feel free to ask in the comments below!