Support » Plugin: Contact Form 7 » Missing trigger?

  • Resolved jifola



    I wanted a function where someone could not submit a duplicate submission, based on the IP address of the current user. And the good news is, it works. Almost.

    You can no longer make the same submission twice, but there seem to be missing some kind of trigger. No error messages pops up and the form doesn`t clear. It works fine when it is not a duplicate submission.

    I made a filter that only pulls the submitted existing data based on the IP address of the current user. And then check if the current submission is a duplicate.

    I am missing a invalid trigger of some kind. Any help would be greatly appreciated thanks!

    The code below. It is heavy on the commenting, but I am not a big coder, so I need to know exactly what is going on. And maybe others could find it usefull 🙂

    // Remember to change url* to what kind of field you are validating. * means it is a required field. Remove the * if it is not.
    add_filter( 'wpcf7_validate_url*', 'my_duplicate_validation', 10, 2 );
    function my_duplicate_validation($result, $tag) {
    // Get the existing data
    require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/CFDBFormIterator.php');
    $exp = new CFDBFormIterator();
    $atts = array();
    $atts['show'] = 'url-submit-input'; // Input name from your form
    $atts['filter'] = 'Submitted From=my_ip_sorting()'; // Custom filter sorting by the current users IP address
    $atts['unbuffered'] = 'true';
    $exp->export($atts['url-submit-form'], $atts); // Name of your form
    $rows = array(); // Make $rows to an array
    while ($row = $exp->nextRow()) {
      $rows[] = $row; // Instead of creating output, just save the data into the array $rows made just before
    // Get the current contact form
    $wpcf7 = WPCF7_ContactForm::get_current();
    // Get the submission, which is generated when the user hits the submit button
    $submission = WPCF7_Submission::get_instance();
    // if a submission is made
    if ($submission) {
    // Put the content of the submission into a variable called $posted_data
    $posted_data = $submission->get_posted_data();
    // Exit the function if the contact form is empty
    if ( empty ($posted_data))
    // Put the content of a single input into a varible called $tag instead of the whole content from $posted_data
    // You can change the name of $tag, but make sure you change it everywhere
    // The name url-submit-input is the name of a input in my contact form
    $tag = $posted_data['url-submit-input'];
    // Check if the value that is submitted is already in the array of values that you pulled from your existing data
    // Url-submit-input is also the name of the field that the data is stored in. Dont confuse it with the name of the input in your form	
    // if(in_array(value to look for, array_column(array to look in, array column to look in)))
    if(in_array($tag, array_column($rows, 'url-submit-input'))) {
        // If value submitted in form is found in the data saved in the array $rows. Then it is a duplicate by the current IP address
        // Invalidate and return error
        $result->invalidate($tag, "error" );
    } else {
    	// If value submitted in form is NOT found in the data saved in the array $rows then it is not a duplicate. Allow the form to send
    	// Remember it is based on the IP address of the current user "$atts['filter'] = 'Submitted From=my_ip_sorting()';"
    	return $result;
    } // if in_array end
    } // If $submission end
    } // Function my_duplicate_validation end

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Missing trigger?’ is closed to new replies.