Gravity Forms – Spam Filtering

By setting up both options below we should be able to greatly reduce spam.

GravityForms – ReCAPTCHA V3

  • Install plugin “Gravity Forms reCAPTCHA Add-On” https://www.gravityforms.com/add-ons/recaptcha/
  • Generate V3 reCAPTCHA keys on Google, make sure to use dev@ampv.com Google account.
    • Add these V3 reCAPTCHA keys to Gravity>Settings>reCAPTCHA
  • Done, it’s that simple. Be sure to test.

GravityForms validation (block specific words)

Add the following script in the functions.php file. Read more about this here: https://docs.gravityforms.com/gform_field_validation/

We can add more words to the list, but we need to be extremely careful not to block any word that might be used. Please ask manager before adding words.


// block specific words in GravityForms webforms
add_filter( 'gform_field_validation', function ( $result, $value, $form, $field ) {
    GFCommon::log_debug( __METHOD__ . '(): Running...' );
    // Only for Single Line Text and Paragraph fields.
    if ( $field->type == 'text' || $field->type == 'textarea' ) {
 
        if ( $result['is_valid'] ) {
            $stop_words = array( // List of words to not allow in lowercase.
                'viagra',
                'porn',
                'seo',
                'rank',
                'ranking',
                '.com',
                '.ru',
                '.net',
                'http',
                'https',
                'www',
                '.ru',
                '.И',
            );
 
            // Stop Words Counter.
            $stop_words_detected = 0;
 
            // Check field value for Stop Words.
            foreach ( $stop_words as $stop_word ) {
                if ( strpos( strtolower( $value ), $stop_word ) !== false ) {
                    $stop_words_detected++;
                    GFCommon::log_debug( __METHOD__ . "(): Increased Stop Words counter for field id {$field->id}. Stop Word: {$stop_word}" );
                }
            }
 
            if ( $stop_words_detected > 0 ) {
                GFCommon::log_debug( __METHOD__ . "(): {$stop_words_detected} Stop words detected." );
                $result['is_valid'] = false;
                $result['message']  = 'Sorry, you used word that is not allowed. Please review your message and remove any spammy words.';
            }
        }
 
    }
 
    return $result;
}, 10, 4 );