In this tutorial, 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.
Why limit comment length in WordPress?
In our experience of moderating online discussions for the past decade, we have found that most helpful comments are above 60 characters and below 5000 characters in length.
When a person writes a one-word comment, it usually is not very helpful. In most cases, it is spam because the author is simply trying to earn a back link from your site.
However when a person writes a comment above 5000 characters, its usually a rant / complaint that in most cases is not relevant to that particular article.
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 Lengthplugin. Upon activation, simply go to Settings » Control Comment Length to configure the plugin settings.
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. We will use it to check the comment length. 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.
add_filter( 'preprocess_comment', 'wpb_preprocess_comment' ); function wpb_preprocess_comment($comment) { if ( strlen( $comment['comment_content'] ) > 5000 ) { wp_die('Comment is too long. Please keep your comment under 5000 characters.'); } if ( strlen( $comment['comment_content'] ) < 60 ) { wp_die('Comment is too short. Please use at least 60 characters.'); } return $comment; }
We hope this tutorial helped you limit comment length in WordPress.