RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.20% (5.24%*) • Home Loan Fixed: 5.48% (6.24%*) • Fixed: 5.48% (6.24%*) • Variable: 5.20% (5.24%*) • Investment IO: 5.78% (6.81%*) • Investment PI: 5.49% (6.32%*)

Using Akismet in Custom Forms

Using Akismet in Custom Forms

To mitigate the spam submitted to our mailing lists (and those of our clients via our partner plugin), we recently looked at how we could integrate Akismet into our own suite of tools. When looking for the relevant codex page, the first result that popped up via our search was a function written by Binary Moon . The code, largely lifted directly from the Akismet WordPress Plugin with only minor modifications, returns a Boolean result.

The simple functions power comes from its versatility; it can easily be integrated into virtually any form-based WordPress application that accepts Akismet-aware content. Used in conjunction with other tools it goes a long way to completely shielding against unwanted junk submissions.

The function accepts a $content array with name, email, website, and comment values. The returned Boolean is either true (spam) or false (not spam).

PHP Function

1
<?php 
2
/*
3
 Using Akismet in Custom Forms
4
 https://www.binarymoon.co.uk/2010/03/akismet-plugin-theme-stop-spam-dead/
5
 http://www.beliefmedia.com/akismet-custom-forms
6
 
7
 $content['comment_author'] = $name;
8
 $content['comment_author_email'] = $email;
9
 $content['comment_author_url'] = $website;
10
 $content['comment_content'] = $message;
11
*/
12
 
13
function beliefmedia_check_spam($content) {
14
 
15
 // innocent until proven guilty
16
 $isSpam = FALSE;
17
 
18
 $content = (array) $content;
19
 
20
 if (function_exists('akismet_init')) {
21
 
22
  $wpcom_api_key = get_option('wordpress_api_key');
23
 
24
  if (!empty($wpcom_api_key)) {
25
 
26
   global $akismet_api_host, $akismet_api_port;
27
 
28
   // set remaining required values for akismet api
29
   $content['user_ip'] = preg_replace( '/[^0-9., ]/', '', $_SERVER['REMOTE_ADDR'] );
30
   $content['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
31
   $content['referrer'] = $_SERVER['HTTP_REFERER'];
32
   $content['blog'] = get_option('home');
33
 
34
   if (empty($content['referrer'])) {
35
    $content['referrer'] = get_permalink();
36
   }
37
 
38
   $queryString = '';
39
 
40
   foreach ($content as $key => $data) {
41
    if (!empty($data)) {
42
     $queryString .= $key . '=' . urlencode(stripslashes($data)) . '&';
43
    }
44
   }
45
 
46
   $response = akismet_http_post($queryString, $akismet_api_host, '/1.1/comment-check', $akismet_api_port);
47
 
48
   if ($response[1] == 'true') {
49
    update_option('akismet_spam_count', get_option('akismet_spam_count') + 1);
50
    $isSpam = TRUE;
51
   }
52
 
53
  }
54
 
55
 }
56
 
57
 return $isSpam;
58
}

Considerations

  • If you're working outside of WordPress there's a large number of libraries on Github that you can check out.

Download our 650-page guide on Finance Marketing. We'll show you exactly how we generate Billions in volume for our clients.

  E. Australia Standard Time [ UTC+10, Default ] [ CHECK TO CHANGE ]

  Want to have a chat?
 

RELATED READING

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment