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.

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;



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

Was it good/ useful/ a load of old rubbish? Let me know on Mastodon, or BlueSky (or Twitter X if you must).

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.

WordPress News

The latest WordPress updates from the WPBriefs Podcast.

Related Posts

26 Oct 2005

Akismet – comment spam hell

As I mentioned a couple of days ago I managed to find out what was going on with the api keys.The big announcement is that Akismet has been released. Akismet is a comment spam blocker for blogs.Developed by the...
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...
07 Mar 2010

WordPress Http API – Read Content From Other Websites

Recently I have been doing a lot of work with API’s and these often involve loading text content (rss/ xml/ json etc) from another website and then displaying the results – for instance the Twitter feed at the bottom of...