0

On my blog, I'm facing spam comments. Russian language comments, a lot of them. Is there any way to trash those comments automatically that are not in English?

Without any plugin please.

3
  • are you asking how to trash a comment if a function says yes? Or are you asking how to write a function that returns yes if given a comment that contains Russian? Your problem can be broken down into multiple smaller questions, not that recommending a plugin can't be an answer here so you don't have to mention that
    – Tom J Nowell
    Commented Jun 21 at 18:38
  • Hello Tom. I'm facing spam comments though I'm using firewall. I have not got any Russian language comments yet that are not spam. So, I have decided to ignore them. I don't like plugin solution if it can be done with some lines of code. Thanks for your time. Commented Jun 22 at 19:00
  • 1
    This question is similar to: How can I automatically delete comments that contain chinese / russian signs?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem.
    – miken32
    Commented Aug 16 at 22:59

1 Answer 1

2

I don't think it's a good idea to trash a comment just because it's in Russian. I'd recommend using Akisment to prevent/stop spam comments instead of deleting them. Prevention is always better than cure.

If you still want to delete Russian (non-English) comments, add this code to the functions.php file of your (child) theme.

function wpse425768_trash_non_english_comments( $comment_id ) {
    $comment = get_comment( $comment_id );
    $comment_content = $comment->comment_content;

    // Function to detect if the comment is mostly non-English
    if ( wpse425768_is_non_english( $comment_content ) ) {
        wp_trash_comment( $comment_id );
    }
}

function wpse425768_is_non_english( $text ) {
    // Count the number of Latin characters
    $latin_count = preg_match_all( '/[a-zA-Z]/', $text );

    // Count the number of non-Latin characters
    $non_latin_count = preg_match_all( '/[^\x00-\x7F]/', $text );

    // If there are more non-Latin characters than Latin characters, it's likely non-English
    return $non_latin_count > $latin_count;
}

add_action( 'comment_post', 'wpse425768_trash_non_english_comments', 20, 1 );
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.