Using Gravity Forms ‘gform_mailchimp_subscription’ PHP filter

The gform_mailchimp_subscription Gravity Forms PHP filter allows you to modify the Mailchimp 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 the member does not currently exist in Mailchimp.

More information

See Gravity Forms Docs: gform_mailchimp_subscription

Examples

If a subscriber already exists, set them as subscribed

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 ) {
    // Use the language code provided by WPML to set the subscriber language.
    if ( defined( '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 );