高效WordPress代码技巧-纯代码实现非中文垃圾评论的自动屏蔽

作为一位使用了WordPress的老用户,也是一路见证了WordPress的不断成长,很多站长在搭建自己的网站的时候可能第一个想到的就是采用WordPress进行建站,Wordpress无论是安全性还是易用性都非常适合新人。虽然搭建网站容易,可是对于网站中出现的非中文垃圾评论想必是很多新站长必定会碰到的问题,而今天我们就和大家聊聊快速有效的WordPress纯代码屏蔽非中文评论小技巧。我们

关于屏蔽WordPress非中文语言评论的方法网络上也有不少,也有各种大神贴出各种方法及代码,这里也是我们之前在网络上搜集的代码自己也在使用,刚好今天无聊写文章就想着把它分享出来,这样对于还没有及时屏蔽垃圾评论的网站站长可以快速拿来使用。

主题function.php添加以下代码

这里的代码使用err方法,如果您所使用的主题无法使用err方法,可以把err换成wp_die方法,具体就看您网站中的主题使用的是什么方法吧,请合理使用。

<?php
function ssdax_comment_all_post( $incoming_comment ) {
$enpattern = '/[一-龥]/u';
$jpattern ='/[ぁ-ん]+|[ァ-ヴ]+/u';
$ruattern ='/[А-я]+/u';
$krattern ='/[갂-줎]+|[줐-쥯]+|[쥱-짛]+|[짞-쪧]+|[쪨-쬊]+|[쬋-쭬]+|[쵡-힝]+/u';
$arattern ='/[؟-ض]+|[ط-ل]+|[م-م]+/u';
$thattern ='/[ก-๛]+/u';
if(!preg_match($enpattern, $incoming_comment['comment_content'])) {
err( "写点汉字吧! Please write some chinese words!" );
}
if(preg_match($jpattern, $incoming_comment['comment_content'])){
err( "写点汉字吧!Please do not use Japanese!日本語出て行け!" );
}
if(preg_match($ruattern, $incoming_comment['comment_content'])){
err( "写点汉字吧!Please do not use Russians!Savage выйти из Русского Севера!" );
}
if(preg_match($krattern, $incoming_comment['comment_content'])){
err( "写点汉字吧!!Please do not use Korean!하시기 바랍니다 한국 / 한국어 사용하지 마십시오!" );
}
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', 'ssdax_comment_all_post');
?>
请登录后发表评论

    没有回复内容