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
	}

How was it for you? Let me know on BlueSky or Mastodon

(Please) Link to this page

Thanks for reading. I'd really appreciate it if you'd link to this page if you mention it in your newsletter or on your blog.

Related Posts

17 Oct 2012

WordPress Social Network Aggregation

I really like the idea of a Tumblog – and even have one on Tumblr.com – but I don’t promote it anywhere. Conceptually it’s great – but I don’t like not having control over my content.What I would really like...
15 Jan 2010

Building WPVote Part 5 : Programming

I’m nearly finished with the design of my new WordPress voting site, WPVote, so now need to look more closely at the programming.I enjoy dissecting WordPress themes, and since I also like code simplicity and minimalism (and am working with...
16 Sep 2009

What’s new with the Elemental WordPress theme?

Elemental is the upcoming theme framework for Pro Theme Design. It’s been in development for absolutely ages, and the code is really showing it’s maturity, and I am really pleased with the possibilities it opens up.The focus when developing Elemental...
30 Dec 2009

Building WPVote Part 3 : Research

After receiving the WPVote domain and theme from Jean I moved on to stage 3. Researching ‘competitors‘ websites.I must admit I tend not to do too much research when building a website, but this time I did decide to do...
13 May 2010

6 Tips to Build Better WordPress Themes

If you want to make WordPress themes, for clients, to release for free or to sell, then there are a lot of factors you need to take into consideration. Below are some hints and tips that should help ease your...
27 May 2013

WordPress: 10 Years Young, What Does The Future Hold?

WordPress is now 10 years old. I started using wordpress 9 years ago – which means I joined the WordPress community early on. The reason I chose WordPress is simply because of the fabled 5 minute install process – I...