Resources     Blog  

  1. Home
  2. WordPress Tutorials
  3. How to Limit Comment Length in WordPress With or Without a Plugin

How to Limit Comment Length in WordPress With or Without a Plugin

One of the great benefits of using WordPress to power your website is the built-in comment system. This allows visitors to leave comments and provides an easy way for you to engages your readers, resulting in more frequent return visits to your site.

But simply knowing that discussions drive visitor engagement isn’t enough. You also need to ensure that the discussions are useful and beneficial. If they aren’t, your comments section could actually have a negative effect and drive readers away.

Today, I will demonstrate two methods to limit the comment length in WordPress.

Why Limit Comment Length

The comment section can either benefit or hurt your website. If your comment section is not about the content on your website, it is often a detriment. The conversations get out of hand and can very often lead to less civil interactions.

With that in mind, it’s important to know how to limit comment length, as well as how to require a minimum number of characters.

First and foremost, it prevents a single character or one-word responses. These types of comments are simply spam and do not drive the conversation in a meaningful way.

Secondly, limiting comment length will prevent a visitor from leaving a comment that is too long or cutting and pasting an entire article into your comment section. Again, this is something that many spam bots try to do.

Make It Reasonable

With the above points in mind, you do need to avoid going overboard. There is such a thing as too big a minimum and too small a maximum. In most cases, this will take a lot of feedback collecting to get right, just expect it to take a few tries to find that sweet spot.

How to Limit Comment Length in WordPress

There are two ways to set the maximum comment length in WordPress. The first is with a plugin and the second with a template modification (coding). In this tutorial, I will walk you through both methods.

Method 1: Limit Comment Length with Yoast Comment Hack

There are a few different WordPress plugins that make it easy to limit comment length and impose a minimum character limit. In this tutorial, I will be using Yoast Comment Hacks. It does exactly what I want it to do, with the added benefit of a few other useful features.

To begin this method, you will first have to install the plugin.

Installing Yoast Comment Hack

Log in to your WordPress admin section.

In the left column navigation mouse over the “Plugins” link and click the “Add New” link.

Add a new WordPress plugin

In the “Search plugins…” box, enter “Yoast Comment Hacks.”

Search for the WordPress plugin

Once you have located the plugin, click the “Install Now” button. When the plugin has finished installing, click the “Activate” button.

Click to install the WordPress plugin

Using Yoast Comment Hack

To access the plugin settings, mouse over the “Settings” link and click the “Comment Hacks” link.

Accessing the WordPress plugin settings

The first option is to set the minimum number of characters for comments. The default here is 15, and that is a good setting for most uses. It will prevent most one-word answers, while still allowing small phrases like to get posted.

Next, you have the option to edit the error message shown when a comment is too short. You will probably want to change this message to something that is consistent with the overall tone and voice of your website.

Configuring the WordPress plugin

Next, you can set the maximum number of characters for comments. The default of 1,500 is enough for roughly three paragraphs. If you find that to be too short, you can set the value higher. Alternatively, if you find it too long you can lower it.

Note: A good rule of thumb is that 500 characters typically equal one paragraph.

Next, edit the error message shown when a comment is too long, then click “Save Changes”.

Configuring the WordPress plugin, part 2

That’s all you have to do to set the maximum comment length and the minimum number of characters for your comments with the “Yoast Comment Hacks” plugin.

Method 2: Editing A Template

To limit comment length and impose a minimum character limit with a template edit is a bit more involved, but it’s not terribly difficult. If you don’t like the added weight or maintenance of a third-party plugin, this is the way to go.

Alternately, you could insert the code snippet I’m going to use into a custom plugin, which separates it completely from a theme, but for the purposes of this tutorial, I will be working within a theme.

Note: Directly editing the files for your primary theme is NOT recommended!

When you make changes to files in your theme, those changes can (and usually will) be overwritten and lost when the theme is updated. For that reason, the use of a child theme is strongly recommended.

Creating a child theme is not hard by any means, but it will require even more work if you do not already have one in place. For the purposes, of this tutorial, I will already assume you have taken the time to create a child theme.

How to Edit Your Template

To limit comment length I am going to be editing the functions.php file in a WordPress child theme. Since your child theme may not already contain a functions.php file, I am going to show you how to create one as well.

Log in to your cPanel and click on the File Manager icon.

Accessing the file manager via cPanel

Click the “public_html” link to expand the file tree and click through the file tree to get to your child theme.

Expand the file tree to get to your child theme

Click the “+File” link. A popup window will appear. Enter the New File Name: functions.php and click on the “Create New File” button.

Creating a new file in cPanel file manager

Highlight the “functions.php” line (A).

Click the “Edit” link (B).

Click the “Edit” button (C).

Editing a file in cPanel file manager

Paste the following code into the file editing window:

<?php

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

?>

You can see the editable areas in bold, those are where the character limits and error messages can be customized, similar to the customizations made using the plugin.

After pasting the code into the new file, click the “Save Changes” button.

Saving file changes in cPanel file manager

That’s all there is to it.

If you want to edit the code in the future, you can do it from within the WordPress file editor.

Accessing the Coding Area Within WordPress

To access the file editor within WordPress, mouse over the “Appearance” link and click the “Editor” link.

Navigate to the WordPress file editor

Select the theme that your WordPress site uses and click the “Theme Functions” link to edit.

Editing a file in a WordPress theme

From here, you will be able to change the maximum and minimum values directly from WordPress without having to log into your cPanel.

Keep the Conversation Going

The comment section is a great way to engage with your audience, so make sure to always keep replying. This gives visitors more of a reason to check back, which helps retain visitors. Thus, the more likely you are to respond and provide quality content, the more likely you are to keep visitors coming back.

Just make sure to keep the spam at bay with one of the many anti-spam plugins in WordPress. Nothing kills a conversation faster than a spam bot.

Do you engage your readers through the comment section of your website? Do you use any other discussion plugins?

Author: Michael Phillips

Michael Phillips is a web hosting industry veteran, helping people make the most of their web presence since 1995.

Was this article helpful?

Related Articles

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.