Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.