How to set minimum and maximum comment length in WordPress

WordPress

Comments are a very essential part of a blog. Comments encourage bloggers to write more for their engaging readers. It also confirms that readers on your blog are engaging. But the Comment system is also a weak point of WordPress. Most people comment on blogs just to get the backlink. Most of these people just write comments like “Great post,” “Well said,” “thanks for writing” or something similar. I already posted how to stop comment spam by adding a captcha. But captcha can only prevent automatic scripts from posting the comments. It will not stop human comment makers from making spam comments. So, you must think about something to only get genuine comments and more engaging readers.

Limiting the comment length is also a nice way of doing this. You can limit the minimum comment length or maximum comment length. Doing this will force users to write genuine and useful comments only. If still a person tries to flood the comment, you can easily recognize the spam comment.

Best Managed WordPress Hosting

WPEngine is the best and most secure managed hosting provider

How to set minimum and maximum comment length in WordPress

To set the comment length limit, you can either use a plugin or add code to the theme. I guess, using the plugin is the easier way for most of the users. Yoast Comment Hacks is the free WordPress plugin that lets to set minimum and maximum comment length in your blog. You can easily install and set comment length. As the name suggests, this plugin is offered by the same company that has a popular SEO Plugin called Yoast SEO.

set minimum and maximum comment length in WordPress

I recommend setting the minimum comment length to 50 and the maximum comment length to 1000. This range is good to prevent spammers. In the Textarea, you can also write the message which you want to display if the comment does not fulfill specific criteria.

If you do not want to use a plugin, you can add the following code to the functions.php file. It is better if you make a site-specific plugin for adding custom codes. In the future, if your theme gets updates, your custom code will still remain there.

add_filter( 'preprocess_comment', 'twg_preprocess_comment' );
  
function twg_preprocess_comment($comment) {
    if ( strlen( $comment['comment_content'] ) > 1000 ) {
        wp_die('Comment is too long. Please keep your comment under 1000 characters.');
    }
if ( strlen( $comment['comment_content'] ) < 50 ) {
        wp_die('Comment is too short. Please use at least 10 characters.');
    }
    return $comment;
}

This code adds a filter hook to preprocess_comment and sets the comment length. This code sets minimum length to 50 and maximum length to 1000.

I hope you like this article. Please do not forget to share this article with your friends on various social media websites. If you have anything to ask, you can comment below and ask.


Deepanker Verma is an experienced WordPress developer who has been working on WordPress for more than 12 years. On TheWPGuides, he writes about WordPress, WordPress development, and WordPress plugins.


Similar Articles

0 Comments

Leave a comment

Comment policy: We love comments and appreciate the time that readers spend to share ideas and give feedback. However, all comments are manually moderated and those deemed to be spam or solely promotional will be deleted.

© 2022 The WP Guides Developed By Deepanker