一些wordpress資源網站為了活躍網站氛圍,提升網站整體質量,便將一些資源設置為評論后下載,這樣的想法時好的,但總會有一些用戶單純的想在你的wordpress網站上下載資源,不愿意好好的評論,總是灌水,很多站長也是為此頭疼不已。這一次的wordpress開發(fā)教大家如果限制評論的字數。
- 參考地址: http://naivet.xintheme.cn/wp_course/27.html

于是我想了一個辦法,我打算限制一下各位大佬的回復評語的數量,我就在百度上面搜索除了這么一段代碼,添加在 wordpress主題根目錄下的 function.php
文件中<?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;
}
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');
添加完以上代碼后,就可以在一定程度上避免一些無意義的評論。如果你的網站沒有評論下載功能的話,可以看看這個wordpress開發(fā)教程:
如果還想要強硬一點,還可以設置為一篇wordpress網站的文章只能評論一次: