Custom filter in PHP and CF7 Custom validation
-
Hello,
and thank you for creating this amazing plugin. I have never seen more extensive documentation. Awesome!I have been trying to make a custom validation for Contact Form 7, but without luck. I have tried a 100 different configurations but just can`t seem to make it work.
I only have one form and one input. I wan`t to make sure that a user cannot submit the same link twice, based on current user IP address.
I have created a custom filter using Shortcodes, Actions and Filters.
And it works if i apply it to a table. It gets the current user IP address.Shortcodes, Actions and Filters custom filter code:
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');
And the form that it is applied to:
[cfdb-datatable form="url-submit-form" id="from_current_ip_table" show="Submitted From,url-submit-input" header="false" filter="Submitted From=my_ip_sorting()"] [/cfdb-datatable]
I have made a test page if you want to take a look. Offcourse you have to make a submission to see your submission. Just copy paste a link from the frontpage if you want to. This only shows the submission made by the current user IP.
The idea is to apply this filter to the Contact Form 7 custom validation. I have used this example https://cfdbplugin.com/?page_id=904 and had a look at this https://contactform7.com/2015/03/28/custom-validation/ and all relating threads.
This is the code for the custom Contact Form 7 validation that i enden up with, before i decided to ask on this forum. Didn`t make many changes to the code from page904 in this example, but I did try a lot of variations.
function is_already_submitted($formName, $fieldName, $fieldValue) { require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/CFDBFormIterator.php'); $exp = new CFDBFormIterator(); $atts = array(); $atts['show'] = $fieldName; $atts['filter'] = "$fieldName=$fieldValue&&Submitted From=my_ip_sorting"; $atts['unbuffered'] = 'true'; $exp->export($formName, $atts); $found = false; while ($row = $exp->nextRow()) { $found = true; } return $found; } function my_validate_url($result, $tag) { $formName = 'url-submit-form'; // Change to name of the form containing this field $fieldName = 'url-submit-input'; // Change to your form's unique field name $errorMessage = 'Email has already been submitted'; // Change to your error message $name = $tag['name']; if ($name == $fieldName) { if (is_already_submitted($formName, $fieldName, $_POST[$name])) { $result->invalidate($tag, $errorMessage); } } return $result; } add_filter('wpcf7_validate_url*', 'my_validate_url', 10, 2);
Even without the custom filter i cant make it work. What am I doing wrong? I can still keep making the same submission over and over again. My form name is “url-submit-form” and my input “url-submit-input”. “Submitted From” shouldn`t be needed because of the current user IP filter.
I made the filter because as far as i have read you can not search the database in sets. URL and corresponding IP address. So the current user IP filter does that job.
I have gone through all of your support threads relating to this and still no luck. I hope you can help. Thank you.
- This topic was modified 5 years, 8 months ago by .
- This topic was modified 5 years, 8 months ago by .
The page I need help with: [log in to see the link]
- The topic ‘Custom filter in PHP and CF7 Custom validation’ is closed to new replies.