Home » Blog » WordPress » Limit Comment Length in WordPress

How to Limit Comment Length in WordPress

Are you looking for a way to limit comment length on your WordPress website? So this WordPress tutorial post is for you.

Limiting the comments word count on your WordPress site helps you boost meaningful discussion and reduce spam comments.

Spammers often hit your WordPress website and drop comments with adult and malicious links. It is very frustrating for every website owner to remove these spam comments.

Meaningful and relevant comments always take the topic to the next level and increase engagement. So in this tutorial, I’ll show you a simple way to limit WordPress comments using a PHP code snippet.

You can add this code snippet using the Code Snippets WordPress plugin or your theme’s functions.php file.

Why limit comment length in WordPress?

Relevant comments always increase engagement and the comment section is a great way to connect with your audience. Your users can leave their thoughts, ask for help, or give feedback on a specific topic.

However, this does not mean that all the comments users leave in the comment section will be meaningful.

Often spammers send meaningless long texts, as well as some spam links to your site. So limiting comment word count can help reduce spam.

Here are some reasons why you might want to limit comment length:

  • To reduce comment spam
  • To make your comments meaningful
  • To increase engagement and relevant thoughts

Now let’s see how to apply PHP code snippets on WordPress:

How to limit comment length in WordPress

You can limit comment length in WordPress by adding code to your theme’s functions.php file. A child theme is the best way to implement a custom code on your website without breaking the parent theme.

Using a plugin is the best way for beginners to implement the code.

I’m using the GeneratePress child theme to add any custom code to my website.

Now let’s do this:

  • Step 1: Log in to the WordPress dashboard, go to Plugins > Add New Plugin, and search for Code Snippets. Now install and activate the Code Snippets plugin.
  • Step 2: Go to Code Snippets and click the Add New Snippets button.
  • Step 3: Give the title of the Snippet and copy-paste the following code into the code box.
/* Limit the comment length to 800 characters and a minimum of 150 characters in WordPress */
add_filter( 'preprocess_comment', 'wpsettingbox_limit_comment_length' );
function wpsettingbox_limit_comment_length( $comment ) {
 
/* Limit the comments to 800 characters */
 if ( strlen( $comment['comment_content'] ) > 800 ) {
 wp_die('The comment is too long. Comments must be under 800 characters. Please go back and remove some characters.');
 }
 
/* Require 150 characters to leave a comment */
 if ( strlen( $comment['comment_content'] ) < 150 ) {
 wp_die('The comment is too short. Comments must be at least 150 characters. Please go back and add more characters.');
 }
 return $comment;
}

Now, click on the “Save Changes and Activate” button to apply the code.

Conclusion

Limiting comment length in WordPress helps keep discussions on your site focused and respectful. By setting the maximum character count, you can prevent excessively long comments that may be off-topic or difficult for others to read.

You can do this easily using a plugin or adding custom code to your theme. Either method gives you control over the length of comments, making your site more user-friendly and easier to manage.

Using these methods helps ensure that comments remain relevant and contribute positively to the conversation on your site.

Photo of author

Dipak Singh

Dipak is an expert SEO Content Writer and WordPress Web Designer. With years of experience, he focuses on simplifying complex processes for better understanding. His motivation behind everything is to help people grow.

Leave a Comment

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