很多资源网站有很多用户为了下载资源会随意评论,有的人会回复一两个字来敷衍一下,要想用户多评论几个字,就需要控制评论最少字数,那么如何控制WordPress文章评论最少字数及最多字数?

实现起来也挺简单的把下面的代码添加的WordPress主题的function.php文件中即可。

add_filter( \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’preprocess_comment\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’, \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’minimal_comment_length\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ );

function minimal_comment_length( $commentdata ) {

$minimalCommentLength = 40;

if ( strlen( trim( $commentdata[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’comment_content\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’] ) ) < $minimalCommentLength )

{

wp_die( \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’抱歉,您的评论太短了,请至少输入 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ . $minimalCommentLength . \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ 个字!\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ );

}

return $commentdata;

}

/*

* WordPress控制文章评论最少字数

*/

function lxtx_set_comments_length($commentdata) {

$minCommentlength = 100; //最少字數限制,建议设置为5-10个字

$maxCommentlength = 2200; //最多字數限制,建议设置为150-200个字

$pointCommentlength = mb_strlen($commentdata[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’comment_content\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’],\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’UTF8\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’); //mb_strlen 一个中文字符当做一个长度

if ( ($pointCommentlength < $minCommentlength) && !is_user_logged_in() ){

err(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’抱歉!您说的内容太少了,请给我讲个\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ . $minCommentlength .\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’个字的笑话(目前字数:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’. $pointCommentlength .\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’)【登录后无此限制】\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’);

exit;

}

if ( ($pointCommentlength > $maxCommentlength) && !is_user_logged_in() ){

err(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’对不起,你的文采太好了,你的笑话太长了,我只要\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ . $maxCommentlength .\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’个字的笑话!(目前字数:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’. $pointCommentlength .\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’)【登录后无此限制】\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’);

exit;

}

return $commentdata;

}

add_filter(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’preprocess_comment\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’, \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’lxtx_set_comments_length\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’);

相关新闻

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

联系我们

联系我们

151-1101-0018

在线咨询:
邮件:30115776@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
关注微信
分享本页
返回顶部