最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
wordpress评论字数限制的方法
时间:2015-07-08 编辑:简简单单 来源:一聚教程网
wordpress的评论这块,有人忧愁有人喜,忧愁的是频繁的广告好闹心,喜的各种限制操作已经让评论很安静,比如大叔,哈哈,前几天墨手说奶子的站存在评论XSS漏洞。。。我急忙的说,来试试我的呢,他尽然告诉我,“不行,你的限制太多了”,那么是什么黑科技让评论如何的安心呢?请进《wordpress垃圾评论的新战术》,但是,今天说的,是wordpress评论字数限制,从而更加严格的规范了评论者的行为!
那么直接上今天的黑科技吧,将下列代码插入到主题的functions.php内,
wordpress默认评论方式:
/* 设定评论字数限制开始 */
function set_comments_length($commentdata) {
$minCommentlength = 3; //最少字数限制
$maxCommentlength = 1000; //最多字数限制
$pointCommentlength = mb_strlen($commentdata['comment_content'],'UTF8'); //mb_strlen 1个中文字符当作1个长度
if ($pointCommentlength < $minCommentlength){
header("Content-type: text/html; charset=utf-8");
wp_die('抱歉,您的评论字数过少,请至少输入' . $minCommentlength .'个字(目前字数:'. $pointCommentlength .'个字)');
exit;
}
if ($pointCommentlength > $maxCommentlength){
header("Content-type: text/html; charset=utf-8");
wp_die('对不起,您的评论字数过多,请少于' . $maxCommentlength .'个字(目前字数:'. $pointCommentlength .'个字)');
exit;
}
return $commentdata;
}
add_filter('preprocess_comment', 'set_comments_length');
/* 设定评论字数限制结束 */
Ajax评论方式:
/* 设定评论字数限制开始 */
function set_comments_length($commentdata) {
$minCommentlength = 3; //最少字数限制
$maxCommentlength = 1000; //最多字数限制
$pointCommentlength = mb_strlen($commentdata['comment_content'],'UTF8'); //mb_strlen 1个中文字符当作1个长度
if ($pointCommentlength < $minCommentlength){
err('抱歉,您的评论字数过少,请至少输入' . $minCommentlength .'个字(目前字数:'. $pointCommentlength .'个字)');
exit;
}
if ($pointCommentlength > $maxCommentlength){
err('对不起,您的评论字数过多,请少于' . $maxCommentlength .'个字(目前字数:'. $pointCommentlength .'个字)');
exit;
}
return $commentdata;
}
add_filter('preprocess_comment', 'set_comments_length');
/* 设定评论字数限制结束 */
区分自己的评论方式,然后创作吧,骚年!
后面网站还整理了一些例子
首先把下边的代码放到当前主题的 functions.php 里,注意修改最小字数:
function Bing_minimal_comment_length( $commentdata ){
$minlength = 20;//评论最少字数
preg_match_all( '/./u', trim( $commentdata['comment_content'] ), $maxlength );
$maxlength = count( $maxlength[0] );
if( $maxlength < $minlength ) wp_die( '评论最少需要 ' . $minlength . ' 字!' );
return $commentdata;
}
add_filter( 'preprocess_comment', 'Bing_minimal_comment_length', 8 );
此代码还支持中文字数哦!
-
上一个: 给wordpress评论处添加表情及工具
相关文章
- WordPress 发表评论只需输入评论者名称教程 11-18
- WordPress博客防止垃圾评论骚扰的例子 07-18
- 三招教你快速屏蔽WordPress垃圾评论 04-15
- 用什么办法可以限制WordPress评论字数 03-31
- 推荐几种防御wordpress垃圾评论的方法 11-17
- WordPress评论回复带气泡效果的实现代码 07-30