How to Use Akismet in Your Own Plugins and Applications — SeanBluestone.com



How to Use Akismet in Your Own Plugins and Applications

Akismet, for those who don’t know, is an excellent and free plugin which comes installed and activated with each copy of WordPress. When activated, all comments submitted to your blog will pass through Akismets database which checks to see if the comment is recognized as spam. If so the comment is held in a moderation queue for you to check or delete. It’s very useful because if a spam message gets through then you can click ‘spam’ to delete it. Akismet learns from your selection and the next time a similar message is submitted on anyones blog it will likely be treated as spam.

A question I commonly see in the WordPress forums is Can I use Akismet in my own program? and the main benefit is that Akismet isn’t just restricted to WordPress, it can be used in third party plugins or applications. This is excellent if you’re creating or modifying an existing PHP script and it’s likely to be hit by spam. I recently developed a WordPress Link Directory plugin which, like all link directories, suffers from spam problems. In newer versions I offer Akismet and CAPTCHA as two solutions to this problem. If Akismet is chosen, all submitted links are checked against Akismet to see if they are spam and rejected or approved accordingly.

So how do you use Akismet in your own WordPress plugin? It’s very simple. First of all you need to download an Akismet PHP class from this page. A good choice is the PHP 5 class which includes a good document on how to use it. But here are the basic steps using WordPress Link Directory as an example.

First I check to see if the user has set the Use Akismet option to Yes and if so include the Akismet.class.php file:

[sourcecode language="php"]if(get_option(‘wplinkdir_akismet_use’)==’Yes’){
include ‘Akismet.class.php’;[/sourcecode]

If so then I initiate a new Akismet class with whichever domain name the plugin is being used on (by getting the siteurl and Akismet key options. I also check that the key is valid before I initiate the Akismet class:

[sourcecode language="php"] $Key=get_option(‘wplinkdir_akismet_key’);
$SiteURL=get_option(‘siteurl’);

if(!$akismet->isKeyValid($Key)) {
echo “The Akismet key you supplied doesn’t appear to be valid. We cannot use Akismet without a key.”;
}else{
$akismet = new Akismet($SiteURL,$Key);[/sourcecode]

If the key is valid I collect the values I want to check. Normally you would run some safety functions on these $_POST variables like mysql_real_escape_string() etc, but in this case WordPress does this automatically. Once I have these values I set them as the Akismet equivalent values:

$name=$_POST['entry_name'];
[sourcecode language="php"] $email=$_POST['entry_email'];
$url=$_POST['entry_url'];
$description=$_POST['entry_description'];

$akismet->setCommentAuthor($name);
$akismet->setCommentAuthorEmail($email);
$akismet->setCommentAuthorURL($url);
$akismet->setCommentContent($description);[/sourcecode]

Finally all I need to do is run the values through Akismet and check if they are seen as spam or not. I’ve excluded some unrelevant code here but basically if it’s spam we put it in the pending links section for someone to check or delete and if it passes OK we add it straight away:

[sourcecode language="php"] if($akismet->isCommentSpam()){
echo ‘Thanks for adding your site, it will be checked by an admin soon to make sure it is appropriate.’;
}else{
echo ‘Thanks for adding your site. Your link has been added!’;
}
}
}[/sourcecode]

This can be done with virtually any content handled by your plugin or script and Akismet is very good at handling huge amounts of data, so you don’t need to worry. If your script allowed the user to send text messages for example, you would use the text message itself in setCommentContent(); and set the name and email while not setting the setCommentAuthorURL(); at all.

The only thing you need to use Akismet in your own program is an API. You can get one by signing up for WordPress.com (it will be emailed to you). This page has more information.


You can discuss this article and see what others are saying about it in the PHP Discussion Forum
Did you find this article useful? Why not buy Sean a coffee?



0 comments ↓

There are no comments yet...Kick things off by filling out the form below.

Leave a Comment