Forum Replies Created

Viewing 3 replies - 1 through 3 (of 3 total)
  • Found a solution that works 🙂 Posted the answer in another thread.
    https://wordpress.org/support/topic/custom-filter-in-php-and-cf7-custom-validation/#post-11370754

    Holy sh*t it works. If you want to make two factor validation then you can use this. In this case I didn`t want the same IP address to make the same submission twice.

    First create a custom filter using the plugin Shortcodes, Actions and Filters. Only tick of “Activated” NOT shortcode. The following code registers the IP address of the current user.

    
    require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/CFDBPermittedFunctions.php');
    function my_ip_sorting() {
        if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
        //check ip from share internet
        $ip = $_SERVER['HTTP_CLIENT_IP'];
        } elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
        //to check ip is pass from proxy
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        } else {
        $ip = $_SERVER['REMOTE_ADDR'];
        }
    	return $ip;
    }
    cfdb_register_function('my_ip_sorting');
    

    Then place the following code in your functions.php.

    
    // Get existing data
    function my_duplicate_validation($found) {
    	
        require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/CFDBFormIterator.php');
        $exp = new CFDBFormIterator();
        $atts = array();
        $atts['show'] = 'url-submit-input'; // The field name
        $atts['filter'] = 'Submitted From=my_ip_sorting()'; // The custom filter
        $exp->export($atts['url-submit-form'], $atts); // The form name
    	$found = true; // Found is set to true, and will only be returned if a duplicate is found
        $rows = array(); // Put the data you extract into an array called $rows
        while ($row = $exp->nextRow()) {
            $rows[] = $row;
        }
    
    	// Get the form that is currently being used
        $wpcf7 = WPCF7_ContactForm::get_current();
    	
    	// Get the current submission which is generated when the user hits the submit button
        $submission = WPCF7_Submission::get_instance();
    
    	// If submission is being made
        if ($submission) {
    	    
    		// Put the values that is being submitted into an array called $posted_data
            $posted_data = $submission->get_posted_data();
    
    		// Exit the function if the form is empty
            if ( empty ($posted_data))
            return;
    
    		// Put the value of the input named url-submit-input into an array called $duplicate
            $duplicate = $posted_data['url-submit-input'];
    
    		// if(in_array(the value to look for, array_column(the array to look in, the column to look in)))
            if(in_array($duplicate, array_column($rows, 'url-submit-input'))) {
    	
    			// If a duplicate is found return $found which is set to true
    			return $found;
    			
            }
    		
        }
    		
    }
    
    // Contact form 7 custom validation
    function my_validate_url($result, $tag) {
    
    	// The error message is related to the field in your form by the name of url-submit-input
    	if ( 'url-submit-input' == $tag->name ) {
    	
    		// Asking the previous function my_duplicate_validation if $found was returned
            if (my_duplicate_validation($found)) {
    			
    			// If $found was returned then the submission is invalidated
    			// If you are using ajax then only .wpcf7-not-valid-tip will show this error message
                $result->invalidate($tag, "Error your submission is a duplicate");
    		
            }
    		
    	}
    	
    	// Return the result
        return $result;
    }
    add_filter('wpcf7_validate_url*', 'my_validate_url', 20, 2);
    

    I tried for a whole damn week to make this thing work 🙂 If you have any questions just ask.

    I got something that works. Finally 🙂 But it is with a very different approach. My only problem now is getting the error message to show. Any ideas please?

    This is what i got so far. It is a bit heavy on the commenting, but i want everyone to know what is going on.

    
    // 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', 20, 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))
    return;
    
    // 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
    

    And the custom filter made in Shortcodes, Actions and Filters.

    
    require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/CFDBPermittedFunctions.php');
    function my_ip_sorting() {
        if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
        //check ip from share internet
        $ip = $_SERVER['HTTP_CLIENT_IP'];
        } elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
        //to check ip is pass from proxy
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        } else {
        $ip = $_SERVER['REMOTE_ADDR'];
        }
    	return $ip;
    }
    cfdb_register_function('my_ip_sorting');
    

    It took me forever to get to this. I have been going at it for a week now. All i need now is to get the damn error message to display. Any help would be greatly appreciated.

    • This reply was modified 7 months, 2 weeks ago by jifola.
Viewing 3 replies - 1 through 3 (of 3 total)