Would you like to limit comment length in WordPress?
WordPress comments encourage discussions around your blog post. However, you may find that comments that are very brief or overly long are not very helpful.
In this article, we will show you how to limit comment length in WordPress.
Why Limit Comment Length in WordPress?
An active comment area is a great way to build a community around your WordPress blog. Visitors can give feedback, ask questions, offer their own point of view on the topic.
However, not all comments are helpful.
We’ve been moderating WordPress comments for well over a decade. In our experience, we’ve found that most helpful comments are above 60 characters and below 5000 characters in length.
One-word comments are usually not very helpful. In most cases, they are spam comments where the author just wants a backlink from your site.
On the other hand, long comments above 5000 characters are often rants or complaints. Sometimes they’re not even relevant to the article.
Setting comment length limits in WordPress will often improve the quality of your comments. However, there is no built-in way of doing this in WordPress.
Let’s take a look at how to control comment length in WordPress by setting minimum and maximum limits.
How to Limit Comment Length in WordPress
To limit comment length in WordPress, you’ll have to add some code to one of your theme files. If you’re not familiar with using code snippets, then see our guide on how to copy and paste code in WordPress.
Simply add the following code snippet to your functions.php file, a site-specific plugin, or by using a code snippets 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;
}
This code snippet works by adding a filter hook to preprocess_comment
. This filter is run before WordPress saves any comments to the database or performs any pre-processing on submitted comments.
It checks the comment length and displays an error message if it is too short or too long.
We hope this tutorial helped you learn how to limit comment length in WordPress. You may also want to learn how to increase your blog traffic, or check out our list of the best WordPress Plugins to grow your site.
If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.
connie mergel says
The control comment length plugin is no longer available> Do you have any suggestions about another plugin that will do the same thing.
Thanks
WPBeginner Support says
While we don’t have a recommendation at the moment, we will be sure to update this article when we find a plugin we would recommend.
Admin
Binh Pham says
Awsome! Thank you so much for the tip.
WPBeginner Support says
You’re welcome, glad you found our guide helpful
Admin
Efe Obasuyi says
For your information, the “Control Comment Length” is no longer on WordPress directory. Thanks
WPBeginner Support says
Thank you for letting us know, we’ll be sure to keep an eye out for an alternative plugin
Admin
Stephen says
The plugin is no longer available. Update your article!
WPBeginner Support says
Thanks for letting us know, we’ll be sure to look for an alternative
Admin
ghost says
hi,
first thanks for your code
and how can i stay in my page and open a popup message showing the message error ?
thanks
Charlie Lynburg says
I am thrilled to have found this! It’s life-changing in a site like mine!
Can you give me a code to add that EXEMPTS this limit for myself (admins)? Thanks
MGK says
is there any way to add read more tag on comment?
Bharat Sharma says
Hi,
The plugin is no longer in WordPress directory, but the code works very well. However, I want to know is there a way we can “gray out” or “disable” the submit button if the comment length conditions are not met. Right now, what this code do is it allow the user to submit comment and they get an error message if their comment length is not as per code criteria. By greying out the button we will be able to stop users from getting irritated and their will be no need to hit the back button.
stanley says
hi where can i download the wordpress application?
can you give the link?or send it thru my gmail? thanks
Abdul Halim says
Can I do comment length limit 50 to 500 instead of 5000? What will be code for that?
WPBeginner Support says
You can use the same code to set set the character limit to whatever you want.
Admin
paunkumar says
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;
}
Tom says
Is there any way to limit comments, and if the limit is exceeded a new page will be created and the rest of the comment is on a new page?