Send Comments in Foreign Scripts to Spam
Sends comments written mostly in Cyrillic, Chinese, Arabic or another script your readers do not use to the spam queue, where most of them belong.
Opens WPIDE on
Code
$wpide_comment_scripts = array( 'Cyrillic', 'Han', 'Arabic', 'Hangul', 'Thai' );
$wpide_comment_share = 0.3; // Share of letters that marks a comment as spam.
add_filter( 'pre_comment_approved', function ( $approved, $commentdata ) use ( $wpide_comment_scripts, $wpide_comment_share ) {
if ( 'spam' === $approved || empty( $commentdata['comment_content'] ) ) {
return $approved;
}
$text = $commentdata['comment_content'] . ' ' . ( isset( $commentdata['comment_author'] ) ? $commentdata['comment_author'] : '' );
$letters = preg_match_all( '/\p{L}/u', $text );
if ( $letters < 10 ) {
return $approved;
}
$foreign = 0;
foreach ( $wpide_comment_scripts as $script ) {
$foreign += (int) preg_match_all( '/\p{' . $script . '}/u', $text );
}
return $foreign / $letters >= $wpide_comment_share ? 'spam' : $approved;
}, 20, 2 );
Notes
Remove scripts from the list if your community writes in them.
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.