How to set minimum and maximum comment length in WordPress

No matter what’s your reason behind limiting visitors’ comments, you should know that making it so isn’t that hard at all. Maybe your visitors often leave huge comments that transfer your site into a chatting room which you don’t like or they copy and paste jokes from around the web and make important comments disappear in the thread.

If you’re using standard comments system in WordPress, you can limit the length of one very easily.

Before you rush to paste the code we will show you below the article, be sure to let your visitors know their comment length has a limitation. Put yourself in the same situation – you definitely wouldn’t want to find yourself writing few paragraphs before you are told that your comment is limited to 500 characters, right?

OK, let’s get back on the code. The following snippet is going to limit comment length. The code consists of two parts where one limits maximum while the other limits the minimum length. Everything is done before the comment is stored into a database. If the comment is too long or too short, a user will get a simple message which you can modify.

Set minimum and maximum comment length:

  1. Copy the code:
  2. add_filter( 'preprocess_comment', 'wpb_preprocess_comment' );
    
    function wpb_preprocess_comment($comment) {
    
    if ( strlen( $comment['comment_content'] ) > 3500 ) {
    wp_die('Please keep your comment under 3500 characters.');
    }
    
    if ( strlen( $comment['comment_content'] ) < 30 ) {
    wp_die('Please use at least 30 characters for your comment.');
    }
    
    return $comment;
    }
  3. Navigate to your functions.php file
  4. Paste the code, save it and try to write a comment

That was all of it. You are free to change max and min limitation to any number of characters you want. Also, you can change the message which will appear once a visitor exceeds your limitation.

If you want a plugin which will do this, check out how you can limit the comment length by using a plugin.

START YOUR OWN BLOG

This guide is an introduction to mastering the art of blogging. It provides easy to follow steps to start, maintain, and grow your blog.

Read the guide

Leave a Reply

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