Wordpress Minimum Comment Content Length – Fighting Comment Spam

Posted on: August 4th, 2008 by Jeremy Schoemaker

Between showing the top commentators on the sidebar and the contest to win a MacBook Air I have had a lot of experience with fighting comment spam. Also from being a moderator at one of the busiest forums on the internet I have seen many methods people use to jackup their post counts. Many times people just post "I agree" or "please pm me" and I find it extremely annoying.

Forcing people to post longer comments will not help you much with automated spam (I will have tips on that in another post) but it will help you a ton with human spam. The true human spammers who were going to just type "yes" will now type "asdf asdf asdf asdf yes" and now their intent to spam is identified and they can be banned properly. The people who did not intend to spam will simply add a bit more content to their sentence. Thats the hope anyway.

So how to do it? Well I started to poke around the Wordpress files looking for something that made sense and I on line 89 of the wp-comments-post.php file I found this:

PHP:
  1. if ( '' == $comment_content )
  2.         wp_die( __('Error: please type a comment.') );

Aha! That makes sense. Basically that is saying if the comment is empty then invoke the wp_die function and say "Error:please type a comment".

While their is no documentation on the Wordpress function reference for "wp_die", just by trying a comment with no content you can conclude pretty much that wp_die means show a custom page with the message we are passing and then stop.

So I added this right after that:

PHP:
  1. if (strlen($comment_content) <25 )
  2.    wp_die( __('Error: Your Comment Is Too Short. Please try to say something useful.') );

What does this do? strlen is a php function that gets the "string length" then it checks to see if it is less than 25 chars. If it is it sends them to the custom wp_die page with the error message of Error: Your Comment Is Too Short. Please try to say something useful.

Now hacking on your Wordpress files to accomplish a goal is pretty cool and educational BUT the next time you upgrade your Wordpress your changes are going to go bye bye...

The long term way is to build a plugin. Which I asked Joost if he could do it and gave him my code. A couple minutes later he had it done. Here it is for you to download.

Post written by Jeremy Schoemaker

Hi I am Jeremy Schoemaker and ShoeMoney.com is my blog. 99% of the post here are done by me but you will see others occasionally make guest posts. This blog is fun to write but for my day job I run several online companies.

More about Jeremy at http://www.shoemoney.com!

Have You Read:

75 Comments. What Say You?

  1. Red31
    October 22, 2009 at 6:40 pm

    PCO judge who could have a dissenting opinion which could be used by anyone to challenge the order. ,

  2. TechNald
    December 9, 2008 at 12:27 pm

    great work sir! god bless!

    • Daddy53
      October 22, 2009 at 6:44 pm

      Databases can provide you with a wealth of sources to examine, and some of them are even keyword searchable. ,

  3. Nicholas Chase
    August 14, 2008 at 11:17 am

    Jeremy, Excellent! I have just created a blog to learn WordPress, and am using my reviewing products from ClickBank promotions, with the link embedded in the body text of the post. You have a great friend there in Mr. Joost.
    Respectfully, Nicholas [The Guitar Blog] http://nachase.wordpress.com/

  4. twig
    August 14, 2008 at 6:32 am

    I could implement it on my actual posts to ensure they’re long enough too :)

  5. Jeff Finch
    August 12, 2008 at 11:48 am

    Thanks for letting me know about this ;)

  6. Mario
    August 9, 2008 at 4:29 am

    it should be great if we have this code in javascript comment checker also

  7. terminator69
    August 8, 2008 at 6:38 am

    Good post, id like to see if this actually helps with spam or not.

  8. Omar
    August 8, 2008 at 1:20 am

    hahaha good alternative but
    this really work ? :)

    look leshii post hahaha

  9. Jef
    August 6, 2008 at 3:09 pm

    I posted on another story and my comment never showed up. It was rather long. If you can’t read this, it means that this one didn’t show up either.

  10. ShoeMoney
    August 6, 2008 at 1:05 pm

    sure that would be pretty easy ;) Just instead of wp_die do a:

    header(“Location: http://www.example.com/“);

  11. leshii
    August 6, 2008 at 10:17 am

    Thank’s thas’s im looking for

  12. Blue SEO
    August 5, 2008 at 1:34 pm

    Yeah, I agree, you should rickroll him hahaha. Yeah it’s a good thing to have the spam check in place on a Wordpress blog with all of the comment spammers out there.

  13. Dick
    August 5, 2008 at 6:45 am

    I agree, this is a very useful thing. Spam is growing exponentially proportions in recent times. Thank you, Shoe.

  14. Mubin
    August 5, 2008 at 3:41 am

    Is there anyway that you could redirect this haphazard spammer to a youtube video of rick astley?

  15. Instant Community
    August 5, 2008 at 1:37 am

    Yeah thanks for the tips and sharing this lovely piece of code. Fighting Spam is one of the biggest challenges now actually!

  16. Sire
    August 5, 2008 at 1:10 am

    Well blow me down and call me a dumb Aussie Blogger. Isn’t it amazing how something so small can make such a big difference. I reckon I will give this plugin a go. Thanks for the tip, oh and it goes without saying ‘nice blog’ ;)

  17. Agent Magenta
    August 5, 2008 at 12:54 am

    Fighting comment spam is a bit like the cold war – for every advance made the spammers up the ante by finding ways around. Its going to be a long time before human moderation can be replaced but nice plugins like this will help reduce the workload by stopping the majority of the spam.

  18. Zak Show
    August 4, 2008 at 11:33 pm

    aha, lol! very good way to keep spammers away. Thanks j for sharing this. I hope that my comment is Ok :P

  19. Rajaie AlKorani
    August 4, 2008 at 11:15 pm

    I always enjoy snooping around the WordPress files, that’s how I found out how to remove the update nag that shows when a new WordPress version is available.

  20. ajaz
    August 4, 2008 at 10:19 pm

    Cool plug-in to fight a growing issue. I will be adding it to my blogs, it should save a bit of time.

  21. Miguel Verissimo
    August 4, 2008 at 7:45 pm

    Great tip, thanks for sharing and for making it into a plugin. I wonder why simple stuff like that is left out by the wordpress team…

  22. Miguel Verissimo
    August 4, 2008 at 7:43 pm

    I love little hacks like this, thanks for sharing. Wordpress is such a powerfully supported tool, it’s hard to me to understand why simple things like this is left out.

  23. Erica DeWolf
    August 4, 2008 at 7:00 pm

    Great tip- thanks so much for sharing!!!

  24. Ultimate Blogging Experiment
    August 4, 2008 at 6:25 pm

    People that spam others are just Internet stupid. The backlash against these people is getting so big that as soon they will be getting caught faster and faster and going to jail for years. People need to think about the consequences. Only problem is a lot of people don’t care.

  25. Vacation Rentals
    August 4, 2008 at 5:29 pm

    Great idea, I have been getting bombarded by manual comment spammers as of late so it probably won’t help to much since most of them are already writing longer comments.

  26. Melvin
    August 4, 2008 at 5:04 pm

    i thought you dont moderate comments here in your blog… well dp is really a hard task.. well i think they have somehoiw changed their policy there…

  27. TYCP Entertainment Magazine
    August 4, 2008 at 4:53 pm

    I like the way you think. I was thinking “Okay, this is great, but the next upgrade will override these settings”, but voila – you put a link to the plugin version. I’m implementing this right away. I hate when people leave comments like that, if you even call them REAL comments.

  28. Winning Startups
    August 4, 2008 at 4:50 pm

    My site doesnt have nearly the comments yours does I guess its a good and bad thing to have so many people placing comments on your site. I wish there were more “plugins” for blogger sites, or I wish it were easier to change from blogger to wordpress, seems all the good advice is for wordpress blogs.

  29. Talk Binary
    August 4, 2008 at 3:40 pm

    After reading this post I came across an unmoderated comment that said VERY NICE!

    I then realized the link they had was to some bad naughty pictures. XD

    This plugin doesn’t come at a bad time at all!

  30. Justin Cook
    August 4, 2008 at 3:04 pm

    I was one paragraph away from finishing the read, and I thought “man, I should just whip together a plugin for this, let you set the min. comment length”.

    And then your last paragraph deflated me.

  31. Webmaster Forums
    August 4, 2008 at 2:58 pm

    Although it an attempt to fight the spam comments, I doubt if it would be much effective. Spammers would come up with new ways. I am kind of surprised though wordpress does not check comment length!

  32. Dogs
    August 4, 2008 at 2:48 pm

    nothing frustrates us more than battling spam!!!
    Never ending circus!

  33. Monchster
    August 4, 2008 at 2:42 pm

    Genius! I have never though about a plug-in for just that. I have noticed a lot of spammers including jibberish in their commens with tons of links. Can the code also include a max set of chars.?

  34. entrenewbie
    August 4, 2008 at 2:32 pm

    Thanks for the tip along with some great insight on a lot of the real “motivation” to leave a comment on someone’s blog…. leave a comment because you want to participate, not because you want to be recognized!

    thanks again for helping to fight the good fight with this tip.

  35. cheap used cars
    August 4, 2008 at 2:17 pm

    sure, but sometimes less is more.

  36. TEKFIRM
    August 4, 2008 at 1:51 pm

    any ban plugin to be released soon ? :P

  37. ShoeMoney
    August 4, 2008 at 1:26 pm

    and just like that your banned see how easy it is to spot the spammers ;)

  38. TEKFIRM
    August 4, 2008 at 1:18 pm

    Aksimet wont catch human spam , this plugin is for humans and not bot :)

    most bot spam are like 1000words+

  39. meethere
    August 4, 2008 at 1:00 pm

    Now thats something interesting..
    Thanks joost and shoe.

  40. newmediaMike
    August 4, 2008 at 12:59 pm

    Thanks for the tip!

    I only wish I was having this problem so I could implement this hack. Akismet intercepts my spam for me and I have yet to have a short winded poster comment on any of my posts.

    Not that I am calling my readers blowhards! I love my long winded intelligent readers.

  41. Start Blogging
    August 4, 2008 at 12:50 pm

    That’s really smart! I’m not too good with PHP coding, and Aksimet doesn’t always catch spam, so this is perfect.

  42. TEKFIRM
    August 4, 2008 at 12:45 pm

    he should have mentioned you at least in the contributors !

  43. Architecture
    August 4, 2008 at 12:31 pm

    Error: Your Comment Is Too Short. Please try to say something useful.

  44. Andrew
    August 4, 2008 at 12:28 pm

    going to try it…I already have some plugins from Joost’s website running on my blogs. I recommend to anyone that runs WP to use Meta Robots from Joost (much more complete than All in One Seo plugin)

  45. VaBeachKevin
    August 4, 2008 at 12:22 pm

    yeah i was just adding a bunch of spaces between the words. a simple check for that and you should be good to go.

  46. Patrick
    August 4, 2008 at 12:16 pm

    I was wondering when you were going to implement this. But yeah, I think it is counting spaces.

  47. wesley
    August 4, 2008 at 12:15 pm

    Yeah, just add a trim() before you do the strlen() check :)

  48. ShoeMoney
    August 4, 2008 at 12:06 pm

    ahh lamer your using a ton of whitespace =P ill do a check for that too

  49. Geiger
    August 4, 2008 at 12:06 pm

    That’s a good idea! (blank) ;)

  50. VaBeachKevin
    August 4, 2008 at 12:05 pm

    2nd try

  51. Bryn (Internet Is My Life)
    August 4, 2008 at 12:00 pm

    Awesome, I was about to go add the line of code when I kept rerading and saw the plugin! Oh how wonderful…installing now, thanks Jeremy and Joost.

  52. ShoeMoney
    August 4, 2008 at 11:50 am

    hmm it should be working try again

  53. VaBeachKevin
    August 4, 2008 at 11:37 am

    hmmm doesn’t work too well (if you are using it).

  54. River Girl
    August 4, 2008 at 11:18 am

    I received this from Joost in his email blast on Saturday. Very cool indeed.

  55. devtrench
    August 4, 2008 at 11:01 am

    Does it piss you off when people keyword stuff the Name they use when they comment? This didn’t used to annoy me but I’m getting tired of ‘Coral Snake’ and ‘portland oregon web design’ commenting on my blog. My new comment policy is to change the name of the commenter slightly so they can’t get on the top commentators list and stay in no-follow hell.

  56. John Cow
    August 4, 2008 at 10:58 am

    It makes me laugh at how things like this often start at us right in the face and we just move along living with it. Thanks for putting a little thought into this… I have downloaded and installed :)

  57. Lawrence
    August 4, 2008 at 10:57 am

    I agree – comment spam is annoying…much more so, if the comment is not intellectually-stimulating…are you feeling me?

  58. team ray
    August 4, 2008 at 10:51 am

    joost the php master

    good tips shoemoney

  59. filontheroad
    August 4, 2008 at 10:49 am

    Great Tool !!! Thank you for that idea and big thanks to Joost too for making that so quick.

  60. Hustle Strategy
    August 4, 2008 at 10:47 am

    I was tripped up by this here to other day. I added more content to the post. It is neat and should serve the purpose that you intend. The biggest problem with customizing wordpress is the upgrades, but being able to make the plugins is a plus.

  61. Sohan
    August 4, 2008 at 10:40 am

    I would implement it if I had a lot of comments :P I get about 2-3 a week tops.

  62. Wcnktm
    August 4, 2008 at 10:34 am

    Very Useful Plugin. Thank you verymuch for sharing it.

  63. Chris Abernethy
    August 4, 2008 at 10:27 am

    This is a pretty cool plugin, I was checking it out on Joost’s site the other day. It sounds like it will be really helpful for sites like this one where there are comment contests etc., as well as dofollow blogs that don’t want to get filled with ‘mee too’ spam.

  64. narendra.s.v
    August 4, 2008 at 10:24 am

    wow thats what i have thought today as i have some 69000+ comments so far by Akismet but since from the past few days am getting small size comments like “is this is great”, “interesting one” all that links to spam, now i gonno try this out :D

  65. BlackhatWay
    August 4, 2008 at 10:19 am

    Hey, thanks for the tip! I haven’t had a problem like this in my blogs yet, but if I start to see posts like that, I will try this!

Leave a Reply