• Hello, I created a custom wordpress plugin for custom validation of fields in CF7 form submissions. In this custom plugin I have tried these two lines add_filter(‘wpcf7_validate’, ‘custom_validate_field’, 10, 2); and add_filter(‘wpcf7submit’, ‘custom_validate_field’, 10, 2); which I thought would result in custom_validate_field() being called when the CF7 form submit button is pressed. But, my plugin function is not being called. It is activate and was activated via the wordpress plugin dashboard…. My question is what changes do I need to make so that my custom plugin function is called when a CF7 ‘submit’ button is pressed? FYI the code below just checks if zip code is 5 digits as a test, we will be doing more conditional checks that require more customization.

    Plugin Name:  CF7 Validate Plugin
    Description:  Validates zip code and other fields when CF7 form is submitted.
    Version:      1.0
    Author:       ab
    */
    
    function custom_validate_field($result, $tags) {
        alert("submit button clicked, validating zip code ");
        $field_to_validate = 'zip_code'; // Replace with the actual name of the field to validate
        alert("submit button clicked, validating zip code ".$tags[$field_to_validate]);
        // Check if the field is present in the form
        if (!isset($tags[$field_to_validate])) {
            return $result;
        }
    
        $user_input = isset($_POST[$field_to_validate]) ? $_POST[$field_to_validate] : '';
    
        // Validate zip code format (5 digits)
        if ($field_to_validate === 'zip_code' && !preg_match('/^\d{5}$/', $user_input)) {
            $result->invalidate($tags[$field_to_validate], 'Invalid zip code. Please enter a 5-digit zip code.');
        }
        // Add more conditions for other fields if needed
    
        return $result;
    }
    
    add_filter('wpcf7submit', 'custom_validate_field', 10, 2);
    
    ?>

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘custom validation plugin’ is closed to new replies.