gform_akismet_fields

Description

Use this filter to specify the fields that are sent to the Akismet anti-spam service.

Usage

add_filter( 'gform_akismet_fields', 'set_akismet_fields', 10, 4 );

You can also target a specific form by adding the form id after the hook name.

add_filter( 'gform_akismet_fields_6', 'set_akismet_fields', 10, 4 );

Parameters

ParameterTypeDescription
$akismet_fieldsarrayThe data to be sent to Akismet, in the following format:
array(
	'comment_type'         => 'gravity_form',
	'comment_author'       => 'Author name',
	'comment_author_email' => '[email protected]',
	'comment_author_url'   => 'http://www.someurl.com',
	'comment_content'      => 'Some text',
	'contact_form_subject' => 'Contact Us',
	'comment_author_ip'    => '127.0.0.1',
	'permalink'            => 'http://www.someurl.com/contactus',
	'user_ip'              => '127.0.0.1',
	'user_agent'           => 'user agent string',
	'referrer'             => 'http://www.referrer.com',
	'blog'                 => 'http://www.someurl.com',
);
PropertyDefault Source
comment_authorThe first Name type field found on the form
comment_author_emailThe first Email type field found on the form
comment_author_urlThe first Website type field found on the form
comment_contentThe first Paragraph type field found on the form
contact_form_subjectThe form title
ParameterTypeDescription
$formForm ObjectThe form which created the entry.
$entryEntry ObjectThe entry being processed.
$actionstringThe action being performed.

The action triggering the Akismet request: submit, spam, or ham. Since version 2.4.18.5.

Examples

1. Override the comment_content

This example overrides the value in the “comment_content” Akismet field so that is is pulled from another field. (field ID: 1).

add_filter( 'gform_akismet_fields', 'set_akismet_fields', 10, 3 );
function set_akismet_fields( $akismet_fields, $form, $entry ) {
    $akismet_fields['comment_content'] = rgar( $entry, '1' );

    return $akismet_fields;
}

2. Provide the IP address

This example shows how you can pass the IP address to Akismet when it has been removed from the entry using the gform_ip_address filter.

add_filter( 'gform_akismet_fields', 'set_akismet_fields', 10, 3 );
function set_akismet_fields( $akismet_fields, $form, $entry ) {
    // Remove any filters that block the IP address
    remove_filter( 'gform_ip_address', '__return_empty_string' );

    // Retrieve the user's IP address
    $ip = GFFormsModel::get_ip();

    // Add the IP address to the Akismet fields
    $akismet_fields['comment_author_IP'] = $ip;
    $akismet_fields['user_ip'] = preg_replace( '/[^0-9., ]/', '', $ip );

    return $akismet_fields;
}

Placement

This code can be used in the functions.php file of the active theme, a custom functions plugin, a custom add-on, or with a code snippets plugin.

See also the PHP section in this article: Where Do I Put This Code?

Source Code

This filter is located in GFCommon::get_akismet_fields() in common.php.