Australia's Leading Digital Marketing Experts. T. 1300 235 433  |  Aggregation Enquires Welcome

Example Interest Rates: Home Loan Variable: 2.54% (2.74%*) • Home Loan Fixed: 3.74% (3.8%*) • Fixed: 2.48% (2.94%*) • Variable: 2.54% (2.74%*) • Investment IO: 2.3% (3.76%*) • Investment PI: 2.04% (3.8%*)

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.

  AUS Eastern Standard Time (Washington)

  Want to have a chat?
 

RELATED READING

Like this article?

Share on facebook
Share on Facebook
Share on twitter
Share on Twitter
Share on linkedin
Share on Linkdin
Share on pinterest
Share on Pinterest

Leave a comment