如何通过代码过滤WordPress垃圾评论:禁止全英文/日文/俄文/韩文评论
作为累积获客10万人以上的上海SEO推广公司,我们经常遇到WordPress网站被垃圾评论困扰的问题。最近很多客户反映博客突然收到大量全英文评论,很可能是机器人发布的。今天分享几种有效的代码解决方案。
解决方案1:禁止纯英文或日文评论
将以下代码添加到主题的funtions.php文件中最后一个?>前面:
// 屏蔽纯英文评论和纯日文
function refused_english_comments($incoming_comment) {
$pattern = '/[一-龥]/u';
// 禁止全英文评论
if(!preg_match($pattern, $incoming_comment['comment_content'])) {
wp_die( "您的评论中必须包含汉字!" );
}
$pattern = '/[あ-んア-ン]/u';
// 禁止日文评论
if(preg_match($pattern, $incoming_comment['comment_content'])) {
wp_die( "评论禁止包含日文!" );
}
return( $incoming_comment );
}
add_filter('preprocess_comment', 'refused_english_comments');
Solution 1: Block Pure English or Japanese Comments
Add the following code before the last ?> in your theme's funtions.php file:
// Block pure English and Japanese comments
function refused_english_comments($incoming_comment) {
$pattern = '/[一-龥]/u';
// Block English comments
if(!preg_match($pattern, $incoming_comment['comment_content'])) {
wp_die( "Your comment must contain Chinese characters!" );
}
$pattern = '/[あ-んア-ン]/u';
// Block Japanese comments
if(preg_match($pattern, $incoming_comment['comment_content'])) {
wp_die( "Japanese comments are not allowed!" );
}
return( $incoming_comment );
}
add_filter('preprocess_comment', 'refused_english_comments');
解决方案2:禁止多种外语评论
更全面的解决方案,可以同时禁止英文、日文、俄文、韩文、阿拉伯文和泰文评论:
// 禁止全英日俄韩阿泰语评论
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!" );
}
// 其他语言检测代码类似...
解决方案3:禁止评论包含链接
很多垃圾评论会包含推广链接,可以通过以下代码过滤:
//禁止发链接
function wp_comment_post( $incoming_comment ) {
$http = '/[href="|rel="nofollow"|http:\/\/|<\/a>]/u';
if(preg_match($http, $incoming_comment['comment_content'])) {
err( "禁止发链接地址!" );
}
return( $incoming_comment );
}
add_filter('preprocess_comment', 'wp_comment_post');
作为专业的谷歌竞价推广服务商,我们建议将这些代码组合使用,可以有效减少90%以上的垃圾评论。同时提醒大家注意代码安全,避免捆绑木马代码等安全问题。
