skip to Main Content

WordPress Code Snippets


Custom Validation for Email Fields in Contact Form 7

You can add custom validation rules for your fields to ensure the data submitted meets your requirements. Here’s an example of how to validate an email field.

add_filter('wpcf7_validate_email*', 'codebykp_custom_email_validation', 10, 2);
add_filter('wpcf7_validate_email', 'codebykp_custom_email_validation', 10, 2);

function codebykp_custom_email_validation($result, $tags) {
    $tag = $tags[0];
    $email_value = isset($_POST[$tag]) ? $_POST[$tag] : '';

    if (!is_email($email_value)) {
        $result->invalidate($tag, "Please enter a valid email address.");
    }

    return $result;
}


I’m a WordPress developer with 10+ years of experience in WooCommerce and custom plugins. I combine technical expertise with design flair to help you create standout, user-friendly websites. Let’s transform your digital presence!

Best WordPress Hosting
Back To Top
Search