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.
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.
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 );