If you run a WordPress blog, you’ve likely faced the nuisance of spam comments. Recently, I found myself inundated with spam comments in Russian and other non-English languages. To tackle this issue, I discovered a way to automatically trash these non-English comments, reducing the clutter and minimizing the spam.
In this article, I’ll share a simple and effective solution to automatically trash non-English comments and prevent email notifications for them.
The Problem: Non-English Spam Comments
Spam comments are a common problem for many bloggers. While there are various plugins available to combat spam, they often don’t differentiate between languages. For blogs primarily written in English, receiving a flood of comments in other languages can be particularly frustrating. Manually moderating these comments is time-consuming and inefficient.
The Solution: Custom Code to Trash Non-English Comments
By adding a few lines of code to your WordPress theme’s functions.php
file or a custom plugin, you can automatically trash comments that are not in English. This solution also prevents WordPress from sending email notifications for these trashed comments.
Step-by-Step Guide
1. Add the Code to Your Theme’s functions.php
File
Open your theme’s functions.php
file and add the following code:
function wpse425768_pre_comment_approved( $approved, $commentdata ) {
$comment_content = $commentdata['comment_content'];
// Check if the comment is mostly non-English
if ( wpse425768_is_non_english( $comment_content ) ) {
// Set the comment status to 'trash'
return 'trash';
}
return $approved;
}
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_filter( 'pre_comment_approved', 'wpse425768_pre_comment_approved', 20, 2 );
2. How It Works
wpse425768_pre_comment_approved
Function: This function hooks into thepre_comment_approved
filter, which allows you to modify the comment status before it is approved. It checks if the comment content is mostly non-English by counting the number of Latin and non-Latin characters. If the non-Latin characters outnumber the Latin ones, it sets the comment status totrash
.wpse425768_is_non_english
Function: This helper function counts the number of Latin and non-Latin characters in the comment content to determine if it’s non-English.
3. Benefits
- Automated Spam Filtering: Automatically trashes non-English spam comments, saving you time and effort.
- No Email Notifications: Prevents email notifications for trashed comments, keeping your inbox clutter-free.
Caution
The above code will trash all non-English comments, whether spam or genuine. So if you want to allow comments in languages other than English on your website, you shouldn’t apply this code.
Conclusion
By adding this simple code to your WordPress site, you can effectively manage and reduce non-English spam comments. This solution ensures your comment section remains relevant and engaging for your audience while freeing you from the burden of constant moderation.
If you found this guide helpful, feel free to share it with other bloggers facing similar issues.