学习 研究
探索 提升

WordPress非插件屏蔽垃圾评论-拒绝全英文评论

一个老掉牙的屏蔽评论的代码,本来我也不是很在意那些垃圾评论,打不了后台一个插件清理得了,可是最近发现,有些正常的评论也被放到了垃圾评论里,看来Akismet还是会疏忽的,所以毛办法,还得把正常评论捡起来,绕着这么多的垃圾评论着实费劲,前一段时间在网上看到了一个屏蔽非中文评论的代码,心想正好试试,果然使用后效果明显,刚开始还有一些带点中文的垃圾评论,现在垃圾评论基本为0,估计是不想每次都敲点中文字符。

代码如下,直接丢进 functions 文件即可。

//屏蔽全英文评论 https://www.dujin.org/1128.html
function refused_spam_comments( $comment_data ){
$pattern = '/[一-龥]/u';
if(!preg_match($pattern,$comment_data['comment_content'])){
err( "You should type some Chinese word (like \"你好\") in your comment to pass the spam-check, thanks for your patience!" );
}
return( $comment_data );
}
add_filter('preprocess_comment','refused_spam_comments');

那么,我们根据以上代码,进行一下扩展。

屏蔽英文垃圾评论以及日语垃圾评论

//禁止全英文和日文评论 https://www.dujin.org/1128.html
function v7v3_comment_post( $incoming_comment ) {
$pattern = '/[一-龥]/u';
$jpattern ='/[ぁ-ん]+|[ァ-ヴ]+/u';
if(!preg_match($pattern, $incoming_comment['comment_content'])) {
err( "写点汉字吧,博主外语很捉急! Please write some chinese words!" );
}
if(preg_match($jpattern, $incoming_comment['comment_content'])){
err( "日文滚粗!Japanese Get out!日本語出て行け!" );
}
return( $incoming_comment );
}
add_filter('preprocess_comment', 'v7v3_comment_post');

单独屏蔽日语垃圾评论

//单独屏蔽日语垃圾评论 https://www.dujin.org/1128.html
function v7v3_comment_jp_post( $incoming_comment ) {
$jpattern ='/[ぁ-ん]+|[ァ-ヴ]+/u';
if(preg_match($jpattern, $incoming_comment['comment_content'])){
err( "日文滚粗!Japanese Get out!日本語出て行け!" );
}
return( $incoming_comment );
}
add_filter('preprocess_comment', 'v7v3_comment_jp_post');

屏蔽俄语垃圾评论

//禁止北方野蛮人留言(俄语) https://www.dujin.org/1128.html
function v7v3_comment_ru_post( $incoming_comment ) {
$ruattern ='/[А-я]+/u';
if(preg_match($ruattern, $incoming_comment['comment_content'])){
err( "北方野人讲的话我们不欢迎!Russians, get away!Savage выйти из Русского Севера!" );
}
return( $incoming_comment );
}
add_filter('preprocess_comment', 'v7v3_comment_ru_post');

屏蔽阿拉伯语垃圾评论

//禁止真主阿拉留言(阿拉伯语,部分) https://www.dujin.org/1128.html
function v7v3_comment_ar_post( $incoming_comment ) {
$arattern ='/[؟-ض]+|[ط-ل]+|[م-م]+/u';
if(preg_match($arattern, $incoming_comment['comment_content'])){
err( "不要用阿拉伯语!Please do not use Arabic!!من فضلك لا تستخدم اللغة العربية" );
}
return( $incoming_comment );
}
add_filter('preprocess_comment', 'v7v3_comment_ar_post');

屏蔽泰语垃圾评论

//禁止人妖部落留言(泰语) https://www.dujin.org/1128.html
function v7v3_comment_th_post( $incoming_comment ) {
$thattern ='/[ก-๛]+/u';
if(preg_match($thattern, $incoming_comment['comment_content'])){
err( "人妖你好,人妖再见!Please do not use Thai!กรุณาอย่าใช้ภาษาไทย!" );
}
return( $incoming_comment );
}
add_filter('preprocess_comment', 'v7v3_comment_th_post');

老子全部都要!!!

//禁止全英文和日文评论 https://www.dujin.org/1128.html
function v7v3_comment_post( $incoming_comment ) {
$pattern = '/[一-龥]/u';
$jpattern ='/[ぁ-ん]+|[ァ-ヴ]+/u';
$ruattern ='/[А-я]+/u';
$arattern ='/[؟-ض]+|[ط-ل]+|[م-م]+/u';
$thattern ='/[ก-๛]+/u';
if(!preg_match($pattern, $incoming_comment['comment_content'])) {
err( "写点汉字吧,博主外语很捉急! Please write some chinese words!" );
}
if(preg_match($jpattern, $incoming_comment['comment_content'])){
err( "日文滚粗!Japanese Get out!日本語出て行け!" );
}
if(preg_match($ruattern, $incoming_comment['comment_content'])){
err( "北方野人讲的话我们不欢迎!Russians, get away!Savage выйти из Русского Севера!" );
}
if(preg_match($arattern, $incoming_comment['comment_content'])){
err( "不要用阿拉伯语!Please do not use Arabic!!من فضلك لا تستخدم اللغة العربية" );
}
if(preg_match($thattern, $incoming_comment['comment_content'])){
err( "人妖你好,人妖再见!Please do not use Thai!กรุณาอย่าใช้ภาษาไทย!" );
}
return( $incoming_comment );
}
add_filter('preprocess_comment', 'v7v3_comment_post');

卧槽,如果这些根本无法满足你的需求?那我只能说一句,关闭评论吧!

未经允许不得转载:HYBRID » WordPress非插件屏蔽垃圾评论-拒绝全英文评论

评论 3

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #1

    就哈哈哈 :grin:

    heyuan0028 5年前 (2019-02-24) Google Chrome 72.0.3626.109 Google Chrome 72.0.3626.109 Mac OS X  10.14.3 Mac OS X 10.14.3 这家伙可能用了美佬的代理 谷歌浏览器 Mac OS X 10_14_3 回复
  2. #2

    找到~ :neutral:

    heyuan0028 5年前 (2019-02-24) Google Chrome 72.0.3626.109 Google Chrome 72.0.3626.109 Mac OS X  10.14.3 Mac OS X 10.14.3 这家伙可能用了美佬的代理 谷歌浏览器 Mac OS X 10_14_3 回复
  3. #3

    感谢大佬找到我的博客,我今天又被别人推荐来看你的了!

    缙哥哥 5年前 (2019-10-05) Google Chrome 69.0.3497.100 Google Chrome 69.0.3497.100 Windows 10 x64 Edition Windows 10 x64 Edition 来自天朝的朋友 谷歌浏览器 Windows 10 回复