Description
The “gform_mailchimp_subscription” filter in Gravity Forms Mailchimp Add-On modifies the subscription object before it is executed.
Usage
add_filter( 'gform_mailchimp_subscription', 'my_function', 10, 6 );
Parameters
- $subscription array
Subscription arguments. See the Mailchimp API documentation for a full list of supported properties. - $list_id string
Mailchimp list ID. - $form array
The form object. - $entry array
The entry object. - $feed array
The feed object. - $member array
The existing member object. False if member does not currently exist in Mailchimp.
Examples
If a subscriber already exists, set them as subscribed
Doing this will avoid the double opt-in for existing members.
add_filter( 'gform_mailchimp_subscription', 'update_existing', 10, 6 );
function update_existing( $subscription, $list_id, $form, $entry, $feed, $member ) {
if ( $member ) {
$subscription['status'] = 'subscribed';
}
return $subscription;
}
Set the signup timestamp
add_filter( 'gform_mailchimp_subscription', function( $subscription, $list_id, $form, $entry, $feed, $member ) {
if ( ! $member ) {
$subscription['timestamp_signup'] = date( DATE_ATOM );
}
return $subscription;
}, 10, 6 );
Set the Subscriber language
The following example uses the ICL_LANGUAGE_CODE constant provided by the WPML plugin to set the subscriber language.
add_filter( 'gform_mailchimp_subscription', 'gf_set_subscriber_language' );
function gf_set_subscriber_language( $subscription ) {
GFCommon::log_debug( __METHOD__ . '(): running.' );
// Use the language code provided by WPML to set the subscriber language.
if ( defined( 'ICL_LANGUAGE_CODE' ) ) {
GFCommon::log_debug( __METHOD__ . '(): Setting value for ICL_LANGUAGE_CODE as subscriber language: ' . ICL_LANGUAGE_CODE );
$subscription['language'] = ICL_LANGUAGE_CODE;
}
return $subscription;
}
Change email_type to text from the default HTML
add_filter( 'gform_mailchimp_subscription', function( $subscription, $list_id, $form, $entry, $feed, $member ) {
// this will force all subscriptions to text format
$subscription['email_type'] = 'text';
return $subscription;
}, 10, 6 );
Set the skip_merge_validation argument
This example shows how you can instruct the Mailchimp API to skip validation of the merge field values when updating an existing member. This requires Mailchimp Add-On version 5.4.1 or newer.
add_filter( 'gform_mailchimp_subscription', function( $subscription, $list_id, $form, $entry, $feed, $member ) {
if ( $member ) {
$subscription['skip_merge_validation'] = true;
}
return $subscription;
}, 10, 6 );
Since
Added existing member object as $member parameter in 4.1.9.
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 includes/addon/class-gf-mailchimp.php.