Top 6 Recommended eCommerce WordPress Plugin for a T-Shirt Sales Business 2015
May 15, 2015
How to Detect AdBlock Users in WordPress
May 19, 2015

How to Limit Comment Length in WordPress

WordPress comments encourage discussions around your topic. However you may find that comments below a certain length or above a certain length are not very helpful. We will show you how to limit comment length in WordPress, so you can set both minimum and maximum comment length limit for your WordPress site in this article.

Why limit comment length in WordPress?

Set Comment Length Limits in WordPress

We have found that most helpful comments are above 60 characters and below 5000 characters in length in our experience of moderating online discussions for the past decade.

When a person writes a one-word comment, it usually is not very helpful. It is spam because the author is simply trying to earn a backlink from your site in most cases.

However its usually a rant / complaint that in most cases is not relevant to that particular article when a person writes a comment above 5000 characters.

By setting comment length limits in WordPress, you can improve the quality of your comments.

There are two methods to limit comment length in WordPress. The first method requires you to install a plugin. The second method uses a simple code snippet that you can add to your site.

Method 1: Limiting Comment Lenght Using a Plugin

First thing you need to do is install and activate Control Comment Length plugin. Simply go to Settings » Control Comment Length to configure the plugin settings upon activation.

Controlling comment length in WordPress using a plugin

The plugin’s user interface is in German with English. You can set both minimum and maximum number of characters a comment can have. We recommend using 60 for minimum and 5000 for maximum character count.

You can also add messages that will be visible to users when there comments are either too short or too long. The plugin only provides these messages in the German language. You can replace it with your own message.

Method 2: Limit Comment Length Using Code Snippet

The second method is for users who don’t mind dealing with code code. We will add a filter hook to preprocess_comment. This filter is run before WordPress saves any comments to database or runs any other pre-processing on submitted comments. To check the comment length, we will use it. If it is above or below the set comment length parameters, then we will show users an error message.

Simply add this code to your theme’s functions.php file or a site-specific plugin.

01 add_filter( 'preprocess_comment', 'wpb_preprocess_comment' );
03 function wpb_preprocess_comment($comment) {
04     if ( strlen( $comment['comment_content'] ) > 5000 ) {
05         wp_die('Comment is too long. Please keep your comment under 5000 characters.');
06     }
07 if ( strlen( $comment['comment_content'] ) < 60 ) {
08         wp_die('Comment is too short. Please use at least 60 characters.');
09     }
10     return $comment;
11 }

Comment too long error

We hope this article helped you limit comment length in WordPress. You may also want to checkout our guide on 12 vital tips and tools to combat comment spam in WordPress.

Welcome to Wordpress Mind! We aim to be a source of quality information free WordPress themes for WordPress lovers all around the world.

Leave a Reply

Your email address will not be published. Required fields are marked *

CAPTCHA HERE * Time limit is exhausted. Please reload the CAPTCHA.