Treat Gravity Forms Entries in Foreign Scripts as Spam
Marks a Gravity Forms entry as spam when most of its text is written in Cyrillic, Chinese or another script you never receive genuine enquiries in.
Opens WPIDE on
Code
$wpide_gf_blocked_scripts = array( 'Cyrillic', 'Han', 'Arabic', 'Hangul', 'Thai' );
$wpide_gf_script_share = 0.3; // Share of letters that marks the entry as spam.
add_filter( 'gform_entry_is_spam', function ( $is_spam, $form, $entry ) use ( $wpide_gf_blocked_scripts, $wpide_gf_script_share ) {
if ( $is_spam ) {
return $is_spam;
}
$text = '';
foreach ( $form['fields'] as $field ) {
if ( in_array( $field->type, array( 'text', 'textarea', 'name' ), true ) ) {
$text .= ' ' . (string) $field->get_value_export( $entry );
}
}
$letters = preg_match_all( '/\p{L}/u', $text );
if ( $letters < 10 ) {
return false;
}
$foreign = 0;
foreach ( $wpide_gf_blocked_scripts as $script ) {
$foreign += (int) preg_match_all( '/\p{' . $script . '}/u', $text );
}
return $foreign / $letters >= $wpide_gf_script_share;
}, 10, 3 );
Notes
Remove scripts from the list that your customers actually write in.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.