Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.