Using Gravity Forms ‘gform_mailchimp_field_value’ PHP filter

The gform_mailchimp_field_value filter allows you to modify a value before it is sent to the Mailchimp API.

Usage

A generic example of how to use the filter for all forms and fields:

add_filter('gform_mailchimp_field_value', 'your_function_name', 10, 5);

To target a specific form, append the form ID to the hook name (e.g., gform_mailchimp_field_value_FORMID):

add_filter('gform_mailchimp_field_value_10', 'your_function_name', 10, 4);

To target a specific field, append both the form ID and the field ID to the hook name (e.g., gform_mailchimp_field_value_FORMID_FIELDID):

add_filter('gform_mailchimp_field_value_10_3', 'your_function_name', 10, 4);

Parameters

  • $value (string) – The value to be modified.
  • $form_id (integer) – The ID of the form being processed.
  • $field_id (string) – The ID of the field being processed.
  • $entry (Entry Object) – The entry currently being processed.
  • $merge_var_name (string) – The name of the mapped Mailchimp MMERGE field.

More information

See Gravity Forms Docs: gform_mailchimp_field_value

Examples

Format Birthday

This example reformats the birthday into mm/dd format before sending it to Mailchimp. The filter is applied only to field ID 3 in form ID 10.

add_filter('gform_mailchimp_field_value_10_3', 'change_birthday', 10, 4);

function change_birthday($value, $form_id, $field_id, $entry) {
    if (empty($value)) {
        return $value;
    }
    $month = date('m', strtotime($value));
    $day = date('d', strtotime($value));
    $date = $month . '/' . $day;

    return $date;
}

Format Signature

add_filter('gform_mailchimp_field_value', function ($value, $form_id, $field_id, $entry, $merge_var_name) {
    return $merge_var_name == 'SIGNATURE' ? RGFormsModel::get_upload_url_root() . 'signatures/' . $value : $value;
}, 10, 5);

Use Choice Label as Value

This example uses the label instead of the field choice value for Mailchimp. This is useful for cases where you need to use the value for calculations. The snippet is applied only to form ID 3.

add_filter('gform_mailchimp_field_value_3', function ($value, $form_id, $field_id, $entry, $merge_var_name) {
    $fields = array(54, 56);

    if (in_array($field_id, $fields)) {
        $form = GFAPI::get_form($form_id);
        $field = GFAPI::get_field($form, $field_id);
        $value = GFFormsModel::get_choice_text($field, $value);
    }

    return $value;
}, 10, 5);

Placement: This code should be placed in the functions.php file of your active theme.

Since: The base filter was added in version 2.0. The form and field specific versions were added in version 3.7.