How to spam-filter a custom content type with the Akismet plugin?

Akismet – libraries:

First I want to mention that there are many Akismet libraries out there:

http://akismet.com/development/

and here are the API documents:

http://akismet.com/development/api/

Akismet – WordPress plugin:

But as I understand it, you want to use the Akismet WordPress plugin as your library.

The following code snippet is a simple proof of concept, based on the Akismet plugin:

// We assume that Akismet is installed with the corresponding API key
if( function_exists( 'akismet_http_post' ) )
{   
    global $akismet_api_host, $akismet_api_port;

    // data package to be delivered to Akismet (Modify this to your needs)
    $data = array( 
        'comment_author'        => 'Mr. Spam',
        'comment_author_email'  => '[email protected]',
        'comment_author_url'    => 'spamalot.com',
        'comment_content'       => 'Hello Spam World!',
        'user_ip'               => '123.123.123.123',
        'user_agent'            => '',
        'referrer'              => '',
        'blog'                  => 'http://example.com',
        'blog_lang'             => 'en_US',
        'blog_charset'          => 'UTF-8',
        'permalink'             => 'http://example.com/hello-world',
        'is_test'               => TRUE,
    );

    // construct the query string
    $query_string = http_build_query( $data );
    // post it to Akismet
    $response = akismet_http_post( $query_string, $akismet_api_host, '/1.1/comment-check', $akismet_api_port );
    // check the results        
    $result = ( is_array( $response ) && isset( $response[1] ) ) ? $response[1] : 'false';
    // display the result (it can be 'true', 'false' or some error message )    
    printf( 'Is it spam? Akismet says: %s', $result );
}

where I use the akismet_http_post function to post the data to the Akismet servers.

According to the API docs, the following parameters are required:

blog, user_ip, user_agent

and one should be careful regarding the spelling of the referrer parameter 😉

Other useful Akismet functions are for example:

- akismet_get_key()
- akismet_check_key_status()
- akismet_verify_key()

The response I got when testing a real spam comment, was like this:

Array
(
    [0] => Array
        (
            [server] => nginx
            [date] => Wed, 23 Oct 2013 12:44:37 GMT
            [content-type] => text/plain; charset=utf-8
            [content-length] => 4
            [connection] => close
            [x-akismet-server] => 192.0.80.244
            [x-akismet-guid] => 07d0136b53cda37432ff5a7b6d86c843
        )

    [1] => true
)

with the positive (true) spam result.

The next step would be to modify this into a usable function or a class.

Since you’re using some custom form fields, I think you could map them like this:

name                       --> comment_author
email                      --> comment_author_email
address + city + telephone --> comment_content

Leave a Comment