Choose another country or region to see content specific to your location and shop online.
United States
USA

How to Limit Comment Length in WordPress in 10 easy steps

Picture of William Bacchus
William Bacchus

Published: April 30, 2024

Last updated: April 30, 2024

Table of Contents

Comments are a crucial aspect of many WordPress websites, allowing users to share their thoughts, ask questions, and engage in meaningful discussions. However, without proper moderation, they can become a source of spam, overly lengthy responses, or off-topic discussions. To maintain a clean and engaging comment section, it’s essential to implement a system that limits comment length. This helps ensure that discussions remain focused, concise, and easier to moderate.

In this guide, we’ll explore various strategies to limit comment length on your website, ensuring a better user experience and reducing the workload for moderators. We’ll cover all the key steps to create a well-regulated comment section. Whether you’re managing a blog, a news site, or an online forum, these tips will help you maintain a vibrant and respectful community.

Why should I set a comment length limit on my website?

A lively comment section can transform your WordPress blog into a thriving community. It’s a place where visitors can share feedback, ask questions, and express their own perspectives on your content. However, not every comment adds value.

After more than a decade of moderating WordPress comments, we’ve noticed that the most useful contributions typically fall between 60 and 5,000 characters. Comments shorter than 60 characters are often unhelpful—usually spam, where the commenter aims to get a backlink from your site with a single word or phrase. Longer comments exceeding 5,000 characters often turn into lengthy rants or complaints, sometimes diverging completely from the article’s topic.

By setting comment length limits in WordPress, you can enhance the quality of your comments and reduce spam. Unfortunately, WordPress doesn’t offer a built-in feature for this, but you can manage it with a few simple tweaks. Let’s explore how you can set both minimum and maximum comment lengths in WordPress to keep your comment section on track.

How to set a comment length limit on my WordPress website?

To set comment length limits in WordPress, you can insert custom code into your functions.php file. However, even a small mistake while adding code could cause your site to break, rendering it inaccessible.

To avoid these risks, we suggest using WPCode, a plugin specifically designed for safely adding code snippets to your WordPress site. WPCode is one of the most reliable WordPress plugins for this purpose, offering a straightforward way to add custom code without jeopardizing your site.

Installing WPCode:

  1. In your WordPress dashboard, navigate to plugins. Then press add new.
  2. Type WPCode into the search bar and install and activate the plugin that looks like this:
wpcode plugin install
  1. Once activated, hover over code snippets, and then press add snippets.
  2. Press the blue button which reads Add your custom code (new snippet).
  1. Once you’re on the Create Custom Snippet page, you can begin by giving your code snippet a title. This title is for your reference and won’t appear on your website’s front end, so feel free to choose any name you prefer.
  2. After that, select PHP Snippet as the Code Type from the dropdown menu in the upper-right corner of the screen.
  1. After you’ve set up your custom snippet, copy and paste the following code into the Code Preview box:
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;
}

This code snippet functions by attaching a filter hook to preprocess_comment. This filter executes before WordPress saves any comments to its database or processes the submitted comments. The snippet verifies the comment length and generates an error message if it’s either too short or too long. By default, it sets a minimum limit of 60 characters and a maximum of 5,000 characters.

If you want to set a different comment limit, just replace the number 5,000 with your desired maximum limit in the code snippet. Similarly, you can change the minimum limit by replacing 60 with your preferred value.

Moreover, you can customize the message that appears when a comment does not meet the length requirements. To do this, edit the text following the wp_die lines in the code as shown below with your custom message:

  1. Next, scroll down to the ‘Insertion’ section and select ‘Auto Insert’. This mode ensures that your custom code is automatically executed on your website as soon as it’s activated.

If you’d like to limit comment length only on specific pages, you can set that up as well.

  1. Scroll down to the Conditional Logic section and enable it by toggling the Enable Logic switch.
  2. Next, select the Show option from the Conditions dropdown menu and click on the + Add new group button. This allows you to define the specific conditions under which your custom code snippet will be applied.
  1. A new tab will open, allowing you to select ‘Page URL’ from the dropdown menu on the left.
  2. Then, enter the URL of the page where you want to restrict comment length in the field on the right. With this setup, the code snippet will only activate on the page with the URL you’ve specified.

Conclusion

Comments can play a bigger part on your website than you think. They are a great way to foster engagement between your company and individual customers and clients. Managing them properly shows your dedication to the wellbeing and user experience of your website visitors, and only encourages to comment more!

Sign up for free news, tips & offers

Your email is safe with us, we don't spam.

Picture of William Bacchus
William Bacchus
Will joined the Elite team in 2021. He has a background in content writing as well as a keen interest in media journalism. His interests include taijutsu and a immense passion for film and television. He aims to inform as many people as he possibly can about the vast and often confusing nature of web design!

Leave a Reply

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

New customer

New to ELITEWEB.Co? Create an account to get started today.

Registered users

Have an account? Sign in now.

Reseller login

If you’re a reseller, use the button below to sign in. (your reseller account is separate to your regular account)

Reseller login

If you’re a reseller, use the button below to sign in. (your reseller account is separate to your regular account)

New customer

New to ELITEWEB.Co? Create an account to get started today.

Registered users

Have an account? Sign in now.

Sign up to our newsletter for the latest news + a Free WordPress Guide

NEWSLETTER