Skip to content

Instantly share code, notes, and snippets.

@davewardle
Created February 25, 2019 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davewardle/f236cbdf7cbc7e6ce82dc1b80d01deb0 to your computer and use it in GitHub Desktop.
Save davewardle/f236cbdf7cbc7e6ce82dc1b80d01deb0 to your computer and use it in GitHub Desktop.
Basic spam protection for Gravity Forms
<?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