[wordpress评论字数限制]wordpress评论字数限制的方法

更新时间:2019-11-26    来源:WordPress    手机版     字体:

【www.bbyears.com--WordPress】

wordpress的评论这块,有人忧愁有人喜,忧愁的是频繁的广告好闹心,喜的各种限制操作已经让评论很安静,比如大叔,哈哈,前几天墨手说奶子的站存在评论XSS漏洞。。。我急忙的说,来试试我的呢,他尽然告诉我,“不行,你的限制太多了”,那么是什么黑科技让评论如何的安心呢?请进《wordpress垃圾评论的新战术》,但是,今天说的,是wordpress评论字数限制,从而更加严格的规范了评论者的行为!


20150620084659

那么直接上今天的黑科技吧,将下列代码插入到主题的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 );
此代码还支持中文字数哦!

本文来源:http://www.bbyears.com/wangyezhizuo/81064.html