admin管理员组文章数量:1122832
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.
Share Improve this question asked Jun 21, 2024 at 18:13 Morshed Alam SumonMorshed Alam Sumon 1571 gold badge2 silver badges7 bronze badges 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, 2024 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. – Morshed Alam Sumon Commented Jun 22, 2024 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, 2024 at 22:59
1 Answer
Reset to default 2I 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 );
本文标签: spamhow to trash WordPress comments if its not in English
版权声明:本文标题:spam - how to trash WordPress comments if its not in English 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736303004a1931735.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论