Wordpress Minimum Comment Content Length - Fighting Comment Spam
shoemoney
·
·
3 min read
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:
if ( '' == $comment_content )
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:
if (strlen($comment_content) <25 )
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.