Beginner's Guide for WordPress / Start your WordPress Blog in minutes

How to Limit Comment Length in WordPress

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.

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.

An Error Message Is Displayed if a Comment 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.

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us. Here's our editorial process.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit - a collection of WordPress related products and resources that every professional should have!

Reader Interactions

17 CommentsLeave a Reply

  1. The control comment length plugin is no longer available> Do you have any suggestions about another plugin that will do the same thing.

    Thanks

    • 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

  2. For your information, the “Control Comment Length” is no longer on WordPress directory. Thanks

    • Thank you for letting us know, we’ll be sure to keep an eye out for an alternative plugin :)

      Admin

  3. hi,
    first thanks for your code
    and how can i stay in my page and open a popup message showing the message error ?

    thanks

  4. 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

  5. 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.

  6. hi where can i download the wordpress application?
    can you give the link?or send it thru my gmail? thanks

    • 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;
      }

  7. 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?

Leave A Reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.