WordPress comment on the word limit method

Source: Internet
Author: User
Tags comments

WordPress comments This piece, some people are sad, sad is frequent advertising good annoying, hi a variety of restrictions on the operation has made the comments very quiet, such as uncle, Haha, a few days ago, said the breast of the site comments XSS bug ... I hurried to say, to try my, he told me, "No, you have too many restrictions," then what is black technology to make comments how reassuring? Please come into the "WordPress Spam Review of the new tactics," but, today, is WordPress commented on the word limit, so more stringent norms of the reviewer's behavior!


So directly on today's black tech bar, insert the following code into the functions.php of the subject,

WordPress Default Comment Mode:

/* Set Comment word limit start * *
function Set_comments_length ($commentdata) {
$minCommentlength = 3; Minimum word limit
$maxCommentlength = 1000; Maximum word limit
$pointCommentlength = Mb_strlen ($commentdata [' comment_content '], ' UTF8 '); Mb_strlen 1 Chinese characters as 1 lengths
if ($pointCommentlength < $minCommentlength) {
Header ("content-type:text/html; Charset=utf-8 ");
Wp_die (' Sorry, your comments are too few words, please enter at least '. $minCommentlength. ' Words (current words: '. $pointCommentlength. ' Word) ');
Exit
}
if ($pointCommentlength > $maxCommentlength) {
Header ("content-type:text/html; Charset=utf-8 ");
Wp_die (' Sorry, your comments are too many words, please less than '. $maxCommentlength. ' Words (current words: '. $pointCommentlength. ' Word) ');
Exit
}
return $commentdata;
}
Add_filter (' preprocess_comment ', ' set_comments_length ');
/* Set Comment word limit end * *
Ajax Comment Way:

/* Set Comment word limit start * *
function Set_comments_length ($commentdata) {
$minCommentlength = 3; Minimum word limit
$maxCommentlength = 1000; Maximum word limit
$pointCommentlength = Mb_strlen ($commentdata [' comment_content '], ' UTF8 '); Mb_strlen 1 Chinese characters as 1 lengths
if ($pointCommentlength < $minCommentlength) {
Err (' Sorry, your comments are too few words, please enter at least '. $minCommentlength. ') Words (current words: '. $pointCommentlength. ' Word) ');
Exit
}
if ($pointCommentlength > $maxCommentlength) {
Err (' Sorry, your comments are too many words, please less than '. $maxCommentlength. ' Words (current words: '. $pointCommentlength. ' Word) ');
Exit
}
return $commentdata;
}
Add_filter (' preprocess_comment ', ' set_comments_length ');
/* Set Comment word limit end * *
Distinguish your own way of commenting, and then create it, SAO years!

The following Web site also collates some examples


First put the following code into the current theme of the functions.php, pay attention to modify the minimum number of words:


Function Bing_minimal_comment_length ($commentdata) {
  $minlength = 20;//comments least words
 preg_match_ All ('/./u ', trim ($commentdata [' comment_content ']), $maxlength);
  $maxlength = count ($maxlength [0]);
 if ($maxlength < $minlength) Wp_die (' comments are minimal needed '. $minlength. Word ' );
 return $commentdata;
}
Add_filter (' preprocess_comment ', ' bing_minimal_comment_length ', 8);
This code also supports the number of words in Chinese Oh!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.