How to Automatically Spam Comments With a Long URL in Wordpress?



One of the good features of Wordpress CMS is "Comments". Any visitor to your website can share his/her views under comments without even registering. The moderator can then moderate comments if settings are allowed.

But the main problem which every Wordpress website owner faces is that there are many spammers who use some scripts/software to automatically post comments to promote their website. They generally put garbled text in comments text or write positive feedback for their own website, which is of no use to your website productivity.

wordpress-comments

wordpress-comments


On the other hand, their website address is generally long. And that this their main target. They just want to put their website URL on your Wordpress website.

There are many WordPress plugins, which automatically marked such comments as Spam and put them in the separate folder or delete them. If you're using Cloudflare CDN; it also removes Spam comments automatically.

Still, there are some spam comments which manages to bypass all these securities and come to your Wordpress website.

So, one solution is, we add a small code to functions.php file and it will automatically mark those comments as spam which have long website URL - i.e. more than 30 characters.


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

If you want to increase/decrease the website length check, you can do so in the 4th line of code.

Note: I strongly recommend to take backup of your functions.php file before making any changes in that. You can restore the original file in case of any problem. Secondly, do not use Wordpress Editor to edit functions.php file. Instead, use the text editor to edit functions.php file and transfer it using FTP client, like CoreFTP.


Article 9 out of 10 based on 24 ratings. 5 user reviews




Enter your email address:




Search this site: