Skip to content

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

Ben View All

Ben is a lifelong Nintendo fan who also likes to build websites, and develop games. He also buys way too much Lego.

One thought on “How to Use Akismet in your Plugin or Theme to Stop Spam Dead Leave a comment

  1. Hi Ben

    This blogpost is written a few years back, but it is useful to me and i want to give you a shout and say thanks.

    I also like what you have done to the theme your are using, cool that the drawing (of you?) and the share button follow scrolling of the page and stops at a point. (Makes me want to learn more about jQuery and CSS)

Leave a Reply

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