How to limit the length of WordPress messages

WordPress message function (comment function), can encourage users to discuss the contents of the article to discuss the exchange. However, for some serious sites, the message is too long or too short, and is not conducive to communication. In this article, we will show you how to limit the length of WordPress comment message, both to prevent the message content is too short, but also to prevent the message content is too long.

Why limit the length of WordPress comments?
For some of the sites often discussed around a topic, the contents of the comments will be limited to a certain range, but more conducive to discussion, such as comments limited to more than 20 words, 1000 words or less. If someone commented only to write a word, such as “praise”, in fact, not much of a sense. This situation is more like to take links to spam comments. Likewise, if someone’s comments are over 1000 words, the scope of the discussion is largely beyond the scope of an article if it is not made with a lot of spam links. Therefore, by limiting the length of WordPress messages, you can better improve the discussion atmosphere of such sites.

Method 1: Use plugins to limit WordPress message length using plugins is relatively simple
In the WordPress management background, in the [plug-in] -> [install plug-in] page, search Control Comment Length; and then download and activate Control Comment Length plug-in.
Then, in the [Settings] -> [Control Comment Length] page to configure. This is a German plugin, I do not know, but fortunately the interface also includes English translation. In order to facilitate the understanding, I added the Chinese translation in the following figure.

Need to fill in, including the minimum number of words and the maximum number of words, and the message length beyond the limits of the prompt message. Limit the number of words, you can set according to their own website situation. Where the unit is byte, UTF8 encoding in each Chinese characters accounted for 3 bytes, English letters and numbers accounted for 1 byte; so we recommend that you set to 60 and 3000 respectively. If you only want to limit the maximum number of words, you only need to limit the minimum number of words to 1; Similarly, if you only want to limit the minimum number of words, you only need to set the maximum number of words to a large number (such as 99999) can be.
Prompt information content can be set up their own. The contents of the above are German, you should at least replace the same with your WordPress site front language. The message can contain HTML code, so you can define the contents and format of the output prompt. Note that there are three parameters: {CommentLength}, representing the length of the message; {minimalCommentLength}, the minimum number of bytes; {maximalCommentLength}, the maximum number of bytes.

Method 2: Limit WordPress Message Length by Code
If you do not want to use plug-ins, more willing to use code to solve the problem, this approach is more suitable for you. You only need to copy the following code to your wordpress theme funcitons.php file, or put your site dedicated plug-in, you can.

  1. add_filter( ‘preprocess_comment’, ‘wpb_preprocess_comment’ );
  2. function wpb_preprocess_comment($comment) {
  3.     if ( strlen$comment[‘comment_content’] ) > 3000 ) {
  4.         wp_die(‘Comment is too long. Please keep your comment under 3000 characters.’);
  5.     }
  6. if ( strlen$comment[‘comment_content’] ) < 60 ) {
  7.         wp_die(‘Comment is too short. Please use at least 60 characters.’);
  8.     }
  9.     return $comment;
  10. }

It is important to note that the wp_die () function’s quotation marks are messages that are output when the message limit is exceeded, and you can replace it with Chinese (or your site’s corresponding front language). It is important to note that if you want to output Chinese prompts, please remember to save the functions.php file or the website-specific plug-in file as utf8 no bom format to prevent the output garbled; use notepad ++ free editor or everedit charging editor The There is one, each character accounted for 3 bytes, do not forget to convert Oh!