Adventskalender Day 21 – Mini-Plugin: The Learning WordPress Blacklist Plugin

marketpress-adventskalender-21

Probably all of us will know about these really annoying spam bots appearing immmediately after a new post has been published. Nowadays, countless plugins doing a good job to prevent spam. Nevertheless, from time to time the one or other unwanted comment fools these preventing tools.

Since Version 1.2.0 the internal function check_comments of WordPress allows for identifying spam. This function checks based of a “Blacklist” if parts of the comment fields (name, email, URL, comment text, IP and user agent) match the content of this list. If this is the case, the comment will be marked as spam.

However, who wants to maintain such a list by hand nowadays? Wouldn’t it be nice, if the list learns automatically? This is what the mini plugin, we are presenting today, can do!

Activation of the mini plugin

After being activated, our mini plugin includes already existing spam comments to the black list. In order to achieve that we register a callback in the activation hook of plugins:

Our function fetches all comments marked as “spam” and calls internally our function “Add to blacklist”- (see below for more).

Attention: In case you have lots of spam comments, it is possible that the activation takes a long time and it might run into time out.

Keep the blacklist up to date

In a next step, we want that our blacklist expands its knowledge. In order to do so, WordPress offers a very useful hook: transition_comment_status. This hook is called every time if a status of a comment has been changed and transfers the old and the new status as well as the comment itself. As we need each of the three parameters we have to specify that:

[/crayon]

Our callback is as follows:

[/crayon]

Loading the blacklist

At first, we load our blacklist, which is stored in the options table. This list includes in each row an unwanted keyword. In order to work conveniently with that list, we use the php function explode() to split wordwraps (n) and to convert an array.

Extending the blacklist

In order to extend the blacklist, we check if the new status “spam” and if the IP address are not yet present in the blacklist. If this is true, we add the address to the list. Additionally, I inserted a filter chrico_comment_blacklist_add allowing for handing over the old and new status as well as the comment to the blacklist. Therefore this Plugin comes with an easy interface for extending the blacklist with additional field.

Cleaning the blacklist

From time to time comments are marked as spam although they are not. Therefore we have to do it again, but the other way around. In order to keep the plugin flexible I added the filter chrico_comment_blacklist_add.

This check is a little bit more complex:

  1. At first we look if the old status was “spam”.
  2. If this is true, we look if the comment was not deleted or moved to the recycle bin.
  3. Finally, we check, if this address already exists in the blacklist and wasn`t deleted once before manually.

Saving the blacklist

If one of the checks (extension/cleaning) was conducted we perform the update afterwards as well. In order to do so, we convert our array back to the previous list and save it.

Conclusion

This mini plugin allows the blacklist for broadening its knowledge and, therefore, improves to effectively prevent spam in comments. I have restricted it to one IP address on purpose, however, it would be possible to add e.g. the host or the email address to the blacklist.

You can find the mini plugin on Github: Comment Blacklist.

Post Sharing

Author Avatar

Hi! My name is Chris and I enthusiastically develop the web from back-end to front-end at Inpsyde. When I'm not discussing accessibility, you can find me at Google+. In my spare time I study computer science, do a lot of sports, read one book after the other and blog at www.chrico.info.

Also Interesting

slack-blog

New Plugin: Slack Connector - Connect WordPress, WooCommerce and Slack

by Michael Firnkes

Initially we merely wanted to optimize our own Slack-processes. With automated notifications from our MarkettPress shop, the blog and our forums. The resul ...

Read more
vip

Inpsyde is the first WordPress.com VIP partner in Germany

by Michael Firnkes

Automattic has given us the title of WordPress.com VIP Service Partner, the first in Germany, Austria and Switzerland. Worldwide, there are only eleven com ...

Read more
wooexperts

Inpsyde is a WooExperts Gold Partner of WooCommerce

by Michael Firnkes

A few weeks ago, Inpsyde GmbH with MarketPress became a certified WooExperts Gold Partner. This is confirmation from WooThemes of our agency’s know-h ...

Read more
backwpup

BackWPup Pro: Secure WordPress Backup with Google Drive

by Michael Firnkes

The Pro version of our BackWPup plugin supports the backup of WordPress databases and files to Google Drive. But how do you set something like that up? And ...

Read more

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">