Limiting the Length of Comments in WordPress

WordPress

User comments on a WordPress blog post are one way of engaging your readers. Often, these comments start a great discussion, expanding on the topic you blogged about. People may add detailed, interesting comments that are a little on the long side but are still very relevant to the discussion. On the other hand, spammers often leave long, rambling comments that do not really have anything to say, or are filled with web addresses or advertisements. If you seem to get many long comments on your blog that are not helpful, you may want to institute a maximum comment length. This can be done fairly easily; this WordPress how-to will walk you through the steps.

Note that you can also add a minimum length to comments. Sometimes one-word comments can be just as annoying, so you may want to make people at least write a full sentence.

Two Different Options

There are two different options that you can use to control the length of comments. The first uses a plugin called Control Comment Length. It is a free plugin that is pretty quick and easy to set up. The second option is to use a little snippet of code.

Control Comment Length

This plugin can be downloaded from WordPress.org. Once you have downloaded and activated it, simply click on Settings, Control Comment Length to get it set up. Note that this plugin was originally written in German and it still contains instructions in German, but there are English instructions as well.

Your first option is to set the minimum number of characters a comment requires. Surveys have shown that people generally find comments that are 60 characters in length to be the most helpful, but you may want to set the limit a little lower than that.

Next, you will be able to write a custom message to people who write comments that do not contain the minimum number of characters. This message can say whatever you want, but it is helpful to include what the character minimum is.

The next two sections are for the maximum characters – enter the maximum character amount and then a short message to anyone who goes over that amount. Surveys have shown that comments with more than 5,000 characters are considered too long, but you do not have to stick to that number.

Using Coding

If you do not want to use the plugin, you can add some coding to the functions.php file. You will want to create what has called a filter to the preprocess_comment function. WordPress will run this filter before it adds the comments to your posts. Here is what the code looks like:

add_filter( 'preprocess_comment', 'wpb_preprocess_comment' );

function wpb_preprocess_comment($comment) {

if ( strlen( $comment['comment_content'] ) > 5000 ) {

wp_die('Your comment is too long. Comments cannot be longer than 5,000 characters.');

}

if ( strlen( $comment['comment_content'] ) < 60 ) {

wp_die('Your comment is too short. Comments must be at least 60 characters long.');

}

return $comment;

}

Replace 5000 and 60 with your maximum and minimum values. You can also change the messages the user sees to whatever you want.

Share it!Share on FacebookTweet about this on TwitterShare on Google+Email this to someone

Our Recommended Wordpress Web Hosting Provider

WordPress

WHC.CA
Get a 50% Discount by using our link.

WHC.ca

Reliable host, with excellent customer service and an easy one-click Wordpress installation. Our recommendation to host a Wordpress blog.

Leave a Reply

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