Wordpress Snippets

Automatically Spam Comments With a Long URL

Post comments are an important part of any blog, they give your visitors a way to engage with the author of the article. But with the benefit of comments on article there is also a lot of disadvantages especially with WordPress. Comment spam is the main and very common problem with WordPress and every blogger have to deal with it. And since WordPress is so popular, it makes it an easy target for automated software crawling the web look for WordPress and comment on the posts.

If you use WordPress then you must have already been using Akismet anti spam plugin, which is definitely a great way to reduce spam comments. Although it protects your WordPress website from most common comment spam techniques but still some spam pass right through its protection system. So we cannot simply rely on one ultimate solution but it’s always a good idea to use additional tricks that can help our website. Earlier you have seen how to batch delete spam comments, and in this article we will show you a little trick to easily identify spam comments.

The primary objective of comment spammers is to post a URL on your website. And I am sure you must also have noticed that most spam comments have a very long URL. So if we can somehow identify these comments that have very long URL then we can easily reduce spam comments which Akismet was not able to caught by automatically mark them as spam comments.

You can paste this code snippet in your functions.php file. This code will mark comments as spam with a URL longer than 50 characters. Of course you can change this URL character limit based on your requirements.

// automatically spam comments with a long url
function wcs_long_url_comment_spam( $approved , $commentdata ) {
    return ( strlen( $commentdata[ 'comment_author_url' ] ) > 50 ) ? 'spam' : $approved;
}
add_filter( 'pre_comment_approved', 'wcs_long_url_comment_spam', 99, 2 );

source

Hire Us. Or just say Hi!
Need a job? Apply to get one.