Home/ Journal Bens Projects Binary Moon Archives About Ben Gillbanks Subscribe to Binary Moon Updates

Subscribe to Binary Moon Website Updates, it's Free and Easy to Stay in Touch

Search Binary Moon

Search Binary Moon

How to Use Akismet in your Plugin or Theme to Stop Spam Dead

So far I think I have been rather lucky, there hasn't been much spam on WPVote, and that's great. But I know these types of sites can be a haven for spammers, so I have used a few different tricks to stop the spam in its tracks. One of those is adding Akismet support.

The function below is something you can add to your plugin without any customisation. The code is only really useful in certain situations but I think it could be very powerful if used properly.

Akismet can be used to stop spam/ nuisance posts anywhere there is content submitted to your site, for instance, one place I have successfully implemented Akismet is in the built-in Elemental contact form. Many forums also have support for Akismet, and naturally it works with WordPress comments too.

In this case I am using it to shield WPVote's article submissions form from spam.

To keep things simple the function requires the Akismet WordPress plugin to be installed. The code is mostly lifted straight from the Akismet plugin itself, turning it into a more generic function that can be used in any situation.

<?php
function bm_checkSpam ($content) {

	// innocent until proven guilty
	$isSpam = FALSE;

	$content = (array) $content;

	if (function_exists('akismet_init')) {

		$wpcom_api_key = get_option('wordpress_api_key');

		if (!empty($wpcom_api_key)) {

			global $akismet_api_host, $akismet_api_port;

			// set remaining required values for akismet api
			$content['user_ip'] = preg_replace( '/[^0-9., ]/', '', $_SERVER['REMOTE_ADDR'] );
			$content['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
			$content['referrer'] = $_SERVER['HTTP_REFERER'];
			$content['blog'] = get_option('home');

			if (empty($content['referrer'])) {
				$content['referrer'] = get_permalink();
			}

			$queryString = '';

			foreach ($content as $key => $data) {
				if (!empty($data)) {
					$queryString .= $key . '=' . urlencode(stripslashes($data)) . '&';
				}
			}

			$response = akismet_http_post($queryString, $akismet_api_host, '/1.1/comment-check', $akismet_api_port);

			if ($response[1] == 'true') {
				update_option('akismet_spam_count', get_option('akismet_spam_count') + 1);
				$isSpam = TRUE;
			}

		}

	}

	return $isSpam;

}
?>

Usage

To use the function you have to pass an array containing specific values. The array should be formatted something like this:

	$content['comment_author'] = $name;
	$content['comment_author_email'] = $email;
	$content['comment_author_url'] = $website;
	$content['comment_content'] = $message;

Then all you have to do is:

	if (bm_checkSpam ($content)) {
		// stuff here
	}

No Responses to “How to Use Akismet in your Plugin or Theme to Stop Spam Dead” Leave a reply ›

Leave a Reply

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

About me

About BenMy name is Ben Gillbanks. I'm a lover of Video Games, WordPress, Web Development and everything in between.

I have been working on the internet since 1998, and working with computers even longer. I am a hardcore Nintendo fanboy and have owned most of their consoles at one stage or another.

Read more about me on my about page.


Follow Me


Random Link-outs

The Binary Network links to all my websites
bengillbanks.co.uk - Ben Gillbanks

Ben Gillbanks

All my websites under 1 roof, the easiest way to find out what I do

Pro Theme Design - premium WordPress themes

Pro Theme Design

Premium WordPress themes by web design pros (erm... that includes me)

Binary Joy - gaming news and reviews

Binary Joy

Gaming news and reviews

Binary Sun - play free online games

Binary Sun

Play and download free and paid games (many made by me)

Gaming Angel - download and play games online

Gaming Angel

Stacks of shareware games, free to try and cheap to buy