Limit the Length of Comments Left on Your Blog with this Plugin

avatar image

This new WordPress plugin provides a configurable limit on the length of comments left in the comment form, with a character countdown displayed for the user and dynamically updated with each keypress.

More on WordPress plugins

Greg’s WordPress plugins available at CounsellingResource.com can help you to:

Would you like to reach the community of bloggers using particular plugins? Check out the Sustainable Plugins Sponsorship Network.

IMPORTANT UPDATE: This plugin now has a newer version! For information on the version 1.2 release, please see the update announcement here: “WordPress Comment Length Limiter Plugin Updated”. For the 1.1 release, please see: “New Version of Comment Length Plugin Adds Anti-Spam Features”.

[Just take me to the plugin!]

Why a WordPress Plugin?

As I mentioned in the announcement for my previous plugin release (see “Greg’s Threaded Comment Numbering Plugin for WordPress”), we run WordPress blog software in several areas of the site — in the Ask the Psychologist section, in our book reviews, and here in our main blog. We use many bits of custom code to add new features to our blogs, and to help our systems run more smoothly. I am releasing some of these bits of custom code in the form of plugins to help other bloggers.

Limiting Comment Length With a WordPress Plugin

Most of us welcome comments on our blog posts, but that doesn’t necessarily mean that we — or our readers — like to see comments reaching into thousands of words. Readers may be discouraged from commenting themselves (or reading comments at all) when they see very long entries left by others, and during times of high load, the significant performance overheads associated with retrieving and displaying long comments can slow even the speediest dedicated servers.

From the perspective of SEO (search engine optimization), very long comments also dilute the impact of the author’s original post by relegating it to a small proportion of the overall content available on the page.

The primary approach to limiting the total volume of comment material displayed on a page has long been to break up comments across several pages — either via a plugin like Keyvan Minoukadeh’s Paged Comments or now with the built-in paged comment feature introduced in WordPress 2.7.

Greg's Comment Length Limiter plugin configuration page

Length Limiter Options

This plugin provides one more tool by directly limiting the length of any one comment. A lightweight Javascript counter — just 5 lines of inline Javascript, with no gigantic external AJAX libraries to load — lets the user know how many characters they have left to complete their entry. Any additional text which might be inserted beyond the configured limit via cutting and pasting is automatically trimmed to length.

For users without Javascript, the counter degrades gracefully, providing a visual indication of the preferred (but unenforced) length limit, without the countdown feature.

For coders, the plugin provides additional configuration options via direct calls.

Installing Greg’s Comment Length Limiter Plugin

Installation works just like any other WordPress plugin: just unzip the plugin archive and upload it to /wp-content/plugins/ — be sure to upload the entire folder gregs-comment-length-limiter. Then you can activate the plugin via the WordPress ‘Plugins’ menu, and configure options via Settings -> Comment Length Limiter.

How to Use Greg’s Comment Length Limiter Plugin

With just a single line of code, most themes which support WordPress 2.7 will also support the comment length limit provided by this plugin. (Note that themes must include the wp_footer() call in order for this plugin — and many others out there — to work correctly.)

  • Make sure your comment form has a name attribute of commentform. (The default template includes an id with this value, but not a name attribute.)
  • Add the following function call within the textarea tag for your comment area, preferably wrapped in a conditional that tests whether the function exists: <?php gcll_tweak_textarea(); ?>

For example, here is how the WordPress 2.7 default theme’s comment textarea looks after updating the tag to support Greg’s Comment Length Limiter plugin:

<p><textarea name="comment" id="comment" cols="100%" rows="10" tabindex="4" <?php if (function_exists('gcll_tweak_textarea')) gcll_tweak_textarea(); ?>></textarea></p>

Optionally, if you would like to position the countdown box yourself, add the following function call wherever you would like the box to appear within your comment form, again preferably wrapped in a conditional that tests whether the function exists:

<?php gcll_show_limit_box_manually(); ?>

Greg's Comment Length Limiter plugin in use with the default WordPress template

Default Theme with Length Limit

The plugin settings page allows you to specify whether you would like the plugin to place the countdown box automatically, or whether you would prefer to do that yourself. If you specify that the plugin should place the countdown box automatically, that setting will apply even if you also include manual placement code within your theme — in other words, the plugin is intelligent enough not to attempt to do it twice. So, if you’d like to compare your placement with the default placement, you can flip the manual placement setting on and off and view the results, without repeatedly adding and deleting the call from your theme.

Calling WordPress Plugin Functions Directly

For styling purposes, manual calls to display the countdown box, using <?php gcll_show_limit_box_manually(); ?>, can also take one parameter specifying your preferred class for the <span> which encloses the box: <?php gcll_show_limit_box_manually('mypreferredclass'); ?>.

The default <span> class is countdownbox.

Safe Wrapping of Plugin-Dependent Function Calls

I cover safe wrapping of plugin-dependent function calls on the information page for one of my other plugins: “Greg’s Threaded Comment Numbering Plugin for WordPress”.

Deactivating and Uninstalling this WordPress Plugin

You can deactivate Greg’s Comment Length Limiter plugin via the plugins administration page, and your preferences will be saved for the next time you enable it.

However, if you would like to remove the plugin completely, just disable it via the plugins administration page, then select it from the list of recently deactivated plugins and choose “Delete” from the admin menu. This will not only delete the plugin, but it will also run a special routine included with the plugin which will completely remove its preferences from the database.

Frequently Asked Questions About Greg’s Comment Length Limiter Plugin

If my theme doesn’t include wp_footer(), can I still use this plugin?
The plugin relies on wp_footer() to incorporate its inline Javascript into the page, so without that call, the countdown box won’t function.

Revision History

I’ve moved the revision history to the README file distributed with the plugin.

Fine Print for this WordPress Plugin

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 3 as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

Get the Plugin!

You can download the plugin right here: Greg’s Comment Length Limiter Plugin.

About the Author: With an educational background in philosophy and mathematics, as well as in counselling, Dr Mulhauser enjoys publishing CounsellingResource.com, providing online counselling and therapy services, and spending time with his family.

This article was last reviewed by Dr Greg Mulhauser, Managing Editor on Wednesday, 4th February 2009. You can leave a reply below.

The URL of this page is:
http://counsellingresource.com/features/2009/02/04/comment-length-limiter-plugin/

41 Responses (Including 13 Discussion Threads) to “Greg’s Comment Length Limiter Plugin for WordPress”

  1. avatar image
    Neil Prestemon
    11

    Well, it’s not working for me; I’m using a modified version of “Generic Design” template. The box appears, my limit of 1000 is in there, but there’s no response.

    I inserted the code and I get the following printed INSIDE my comment box:
    onkeydown=”gcllCounter(this)” onkeyup=”gcllCounter(this)”

    My comment form’s name is “comment” and so is the id. When I tried to change it to “commentform” – suddenly, it would no longer submit the post. The button would press and nothing.

    So I guess my question is: is there somewhere else I need to change “comment” to “commentform” so that changing this name does not break my submission?


    • 11.1

      Hi Neil,

      As you haven’t mentioned the URL for the site where you’re having trouble, I can’t offer any direct feedback on the site itself. However, from what you’ve described, as far as I can tell, you just need to go back for another take on the instructions…

      The “Generic Design” template, if it’s the one which goes by that name at the WordPress theme repository, does not have a comment form with either a name or an id of ‘comment’ — although it does have a textarea by that name. The form tag, however, has an id of ‘commentform’ but no name. As indicated in the instructions, it needs one.

      Regarding the JavaScript appearing INSIDE your comment box, if you have another peek at the example given in the README file, you’ll see where the code needs to go — not between the tags, but inside the tag. As it happens, the relevant portion of comments.php from your theme is actually identical to the example given in the README file, so you can literally copy and paste from one to the other.

      All the best,
      Greg


  2. avatar image
    Neil Prestemon
    12

    I see what I did wrong. There was an extra closing tag, and I deleted it, but that should have been my clue that I wanted to get rid of the closing tag from the <textarea parameters – I did that, and I added a name parameter to the form. That did the trick.

    I couldn’t send you a url for the site yet, because it’s still in development, and not yet public. We haven’t assigned a public address to it yet; but this plugin feature is very handy – I’m demoing it to my customer so they can see if they want to use it.


  3. avatar image
    Aleksandr
    13

    Hi there! It worked fine with WP 2.8.4 and Arclite 1.5.4 theme. Yes, I had to modify theme’s comments.php. The only problem – in WP admin “Plugins” has a red circle with number “1″ in it righ after activation of the plugin. And plugin’s setting are not available – it only shows small “tools” icon


    • 13.1

      Hi Aleksandr,

      Hmmm, if you check my reply to another recent comment at the following URL, I mention that the ‘disappearing options’ problem only seems to have been reported in cases where WP’s built-in auto-upgrade system has failed to perform properly:

      http://counsellingresource.com/features/2009/01/27/threaded-comment-numbering-plugin-for-wordpress/#comment-50900

      I’d recommend the suggestions from that reply.

      Some people are also getting hassles from WP 2.8’s higher memory requirements. If you’re affected, you can open up your wp-settings.php file and change WP_MEMORY_LIMIT from 32M to 64M.

      With regard to the red circle after “Plugins”, that’s WP’s built-in system letting you know whether any of your plugins need to be updated. If you visit your plugin admin page, you’ll see a notice if there are new versions available of any of your plugins. (That red circled number doesn’t always update straight away. It may take a few more clicks around your admin panel before it shows correctly.)

      All the best,
      Greg


  4. avatar image
    Hector
    14

    I’ve used your plugin in my site…. work fine, but never display the counter diminishing the characters while I am writing.

    I see that in your site the counter is diminishing in real time the “characters available”, What can i do for work this feature?

    Thanks for your quick assistance.


    • 14.1

      Hi Hector,

      Without any further details to go on, the best I can suggest is to double-check that you have followed the instructions fully, including ensuring that your comment form has a name attribute of commentform and adding the required template tag/function call within the appropriate textarea tag.

      All the best,
      Greg


  5. avatar image
    Kevin
    15

    Greg… sounds like a great plugin, and exactly what I need (I’ve got a couple of loyal readers who are wonderful, but are fond of 1000+ word comments. !!) But… I’m not a guy who’s comfortable with code, and can’t get the plugin to work.

    I’m using Vladimir Prelovac’s Amazing Grace theme. I have no idea of how to find whether or not the comment form has the name attribute “commentform”. (I’ve looked through comment.php but can’t find it.) I inserted the code into the appropriate line, but it doesn’t seem to work.

    aaaarrrggghhhh! I may need counseling after all of this! ;-) Can you help??

    Thanks in advance…

    Kevin


    • 15.1

      Hi Kevin,

      I’m afraid I’m not able to provide custom coding or theme modifications for folks using the plugin, but I hope the instructions above (including the explicit code example from the default theme) can be of some help.

      As a general guide, with regard to the name attribute, if you have a peek at your comments.php file in your theme, you’ll find the form tag in it already has id="commentform", and what it needs is that as well as name="commentform".

      Then it’s just a matter of adding the template tag documented in the instructions on this page (namely, gcll_tweak_textarea), and you’re good to go.

      For general assistance with modifying XHTML in general or WordPress theme files in particular, there are loads of free resources out there as well as plenty of paid help available for folks who would just like to get it done without investing lots of their time.

      All the best,
      Greg


  6. avatar image
    Kevin
    16

    Greg,

    Thanks for your response. Good news/bad news: I’ve gone back and tried several things… the plugin seems to work ok at present, except the countdown “character counter” is there, but doesn’t count down. (As I type this, your counter is functioning normally.)

    I tried pasting a 900+ word document as a comment, and when I submitted it, the plugin truncated it, per the settings I’d opted for. So it works (I think), but the counter isn’t.

    Any thoughts?

    Thanks for your time and help!

    Kevin


    • 16.1

      Kevin, as I mentioned, I’m not in a position to provide free custom theme modifications for folks using the plugin. However, if you follow the instructions, I’m confident you’ll be able to get things working properly. (I can tell with a quick look at your site that the form tag does not have the required name attribute, and the textarea tag does not have the required tweak.)

      All the best,
      Greg


  7. 17

    [...] last bit brings me to my blog-update related news. I added a new plugin tonight, “Greg’s Comment Length Limiter” which pretty much does exactly what it says. When I’m quickly perusing the spam folder [...]


  8. avatar image
    Kevin
    18

    I finally got it to work. If you know nothing at all about XHTML, you may struggle a bit. I found this article to be of help: http://is.gd/4wyxX


    • 18.1

      Hi Kevin,

      Hey, that’s excellent — I’m really glad to hear it’s working for you!

      (Beware that the article you mentioned talks about the pre-2.7 way of displaying comments, so if you ever need to modify something using the new code for 2.7 and later, you may want something newer to refer to.)

      Incidentally, your energy bar recipe looks extremely tasty! Yummmm!

      All the best,
      Greg


Page 2 of 2«12

Join the Discussion!

We support Gravatars rated PG or G; if you don't have a Gravatar, we'll display a mathematically created identicon next to your comment.

A valid email address is required to enable you to personally verify and authorize your comment for posting. It will not be displayed in your post or used in any other way. SPAM comments will be deleted, as will those attempting to circumvent the Gravatar rating system or attempting to circumvent our Terms of Use by employing Gravatar images to advertise on this site.

 characters available

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe without commenting