Created
February 25, 2019 17:30
-
-
Save davewardle/f236cbdf7cbc7e6ce82dc1b80d01deb0 to your computer and use it in GitHub Desktop.
Basic spam protection for Gravity Forms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// simple spam protection - drop any submissions where email is in a blacklist | |
// note: this function assumes that form has honeypot field enabled. | |
function pre_validation_handler( $form ) { | |
// blacklist is an array of bad email addresses or spam phrases | |
// for now, just a single (real) spammer | |
$blacklist = [ 'atouchofawesomeness@gmail.com' ]; | |
// loop thru fields looking for spammers | |
foreach( $form['fields'] as &$field ) { | |
// get the submitted value for this field | |
$value = RGFormsModel::get_field_value( $field ); | |
// check if value in array | |
// note: this could be enchanced with a regex check or similar | |
if ( in_array( $value, $blacklist ) ) { | |
// honeypot is not a field at this point in processing, thus we cannot get ID | |
// so lets just do what GF does.. | |
$honeypot_field_id = GFFormDisplay::get_max_field_id( $form ) + 1; | |
// then we set the honeypot value | |
$_POST['input_' . $honeypot_field_id] = 'blacklisted'; | |
// and let GF do the rest | |
return $form; | |
} | |
} | |
return $form; | |
} | |
add_action( 'gform_pre_validation', 'pre_validation_handler' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment