外贸独立站的底层设计密码WordPress 成品站SaaS建站
当前位置:首页>WordPress建站>用户交互>WordPress评论中禁止或必须包含指定内容

WordPress评论中禁止或必须包含指定内容

倡萌在《WordPress 如何有效拦截和过滤垃圾评论》已经分享了多种垃圾评论拦截方法,这篇文章是对那篇文章的方法的进阶。

评论中禁止包含指定内容

以下代码禁止许评论中包含 <a 随便 href=" 或者rel="nofollow"或者http:// ,你也可以根据自己的需要修改:

function lianyue_comment_post( $incoming_comment ) {  
$http = '/[href="|rel="nofollow"|http:\/\/|<\/a>]/u'; 
if(preg_match($http, $incoming_comment['comment_content'])) { 
wp_die( "万恶的发贴机!" );  
}  
return( $incoming_comment );  
}  
add_filter('preprocess_comment', 'lianyue_comment_post');  

评论中必须包含指定内容

比如下面的代码就是评论必须包含中文字符,你可以修改为自己指定的字符:

function lianyue_comment_post( $incoming_comment ) {  
$pattern = '/[一-龥]/u';  
// 禁止全英文评论  
if(!preg_match($pattern, $incoming_comment['comment_content'])) {  
wp_die( "您的评论中必须包含汉字!" );  
}  
return( $incoming_comment );  
}  
add_filter('preprocess_comment', 'lianyue_comment_post');  

评论中禁止或必须包含指定内容

这是上面两种方法的综合,评论必须包含中文字符,且禁止包含 <a 随便 href=" 或者rel="nofollow"或者http://

function lianyue_comment_post( $incoming_comment ) {  
$pattern = '/[一-龥]/u';  
$http = '/[href="|rel="nofollow"|http:\/\/|<\/a>]/u'; 
// 禁止全英文评论 
if(!preg_match($pattern, $incoming_comment['comment_content'])) { 
wp_die( "您的评论中必须包含汉字!" ); 
}elseif(preg_match($http, $incoming_comment['comment_content'])) { 
wp_die( "万恶的发贴机!" );  
}  
return( $incoming_comment );  
}  
add_filter('preprocess_comment', 'lianyue_comment_post');  

修复ajax评论使用本代码出现的错位

如果你的主题使用ajax评论,使用上面的代码后,可能在提示错误信息的时候,网站会错位,你可以打开 comments-ajax.php ,找到最后个err( __(并且跳过该行,在下一行增加:

这是禁止包含的内容

$http = '/[href="|rel="nofollow"|http:\/\/|<\/a>]/u';  
if (preg_match($http,$comment_content) )  
err( __('万恶的发贴机!') );  

这是必须包含中文的

$pattern = '/[一-龥]/u';  
if (!preg_match($pattern,$comment_content) )  
err( __('您的评论中必须包含汉字!') );  

原文:http://www.lianyue.org/2011/2107/

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA打赏
共{{data.count}}人
人已打赏
欢迎关注WordPress大学公众号 WPDAXUE
用户交互

WordPress自动拒绝包含特定关键词的垃圾评论

2013-5-22 6:54:56

用户交互

为WordPress评论表单添加Quicktags按钮

2013-5-25 7:45:18

2 条回复 A文章作者 M管理员
  1. 没有找到这个文件啊 comments-ajax.php

  2. 对于评论中必须带中文 全英文 全日语的 这种现在已经不管用了挡不住高级的机器人的
    给个思路 判断邮箱有没有注册头像 然后还要判断是否存在后台设置评论’黑名单’

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索