Filter Gravity Forms Textarea for Spam

Would you like to prevent more spam by filtering your Gravity Forms textarea fields? Here is a code snippet you can put in your functions.php file. It checks for blacklisted words or phrases in the textarea fields. If found, the form will be invalidated and not submit.

Think about your blacklisted words carefully. I added a space to some words like ‘seo’ to not catch someone’s legitimate word like phraseology. Because if someone is contacting me about phraseology, I definitely want to read that.

// IMPORTANT Replace 64 with your contact form id
add_filter( 'gform_validation_64', 'az_custom_validation');
function az_custom_validation($validation_result){

$form = $validation_result['form'];
$banned = ['http','SEO',' seo', 'link',' rank',' free ',' offer ']; // enter your words here
foreach ($form['fields'] as &$field){
   if ( $field->type == 'textarea' ) {

   $value = rgpost( 'input_'.$field->id );
   foreach ($banned as $word){
    if (strpos($value,$word) !== false){ // case sensitive
    // Set the form validation to false.
    $field->failed_validation = true;
    $field->validation_message = 'We are unable to submit your form due to blacklisted content.'; // Change messaging to suit

    // set the form validation to false
    $validation_result['is_valid'] = false;
    break 2;
   }
}
}

}

// Assign modified $form object back to the validation result.
$validation_result['form'] = $form;
return $validation_result;
}