gform_mailchimp_field_value

Description

Use this filter to modify a value before it is sent to the Mailchimp API.

Usage

The base filter which would run for all forms and all fields would be used like so:

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

To target a specific form append the form id to the hook name. (format: 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. (format: 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.

Examples

1. Format Birthday

This example takes the birthday and reformats it into mm/dd before sending. This time we’re using the form id and field id in the filter name to target only 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 ) {
// Skip processing if $value is empty.
if ( empty( $value ) ) {
return $value;
}
$month = date( 'm', strtotime( $value ) ); //get month
$day = date( 'd', strtotime( $value ) ); //get day
$date = $month . '/' . $day; //build date into format needed by Mailchimp
return $date;
}

2. 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 );

3. Use Choice Label as Value

This example uses the label instead of the field choice value as Mailchimp value. This is useful in cases where you need to use the value for calculations. The example uses the variable $fields to indicate the id of the fields where the snippet must take action.

// Use choice label as value for Mailchimp. Limited to form id 3.
add_filter( 'gform_mailchimp_field_value_3', function ( $value, $form_id, $field_id, $entry, $merge_var_name ) {
	gf_mailchimp()->log_debug( 'Running for field ' . $field_id );

	// Add the id number of your choice based fields here.
	$fields = array( 54, 56 );

	if ( in_array ( $field_id, $fields ) ) {
		gf_mailchimp()->log_debug( 'Getting choice label for field ' . $field_id );
		$form  = $form = GFAPI::get_form( $form_id );
		$field = $field = GFAPI::get_field( $form, $field_id );
		$value = GFFormsModel::get_choice_text( $field, $value );
		gf_mailchimp()->log_debug( 'Value set to ' . $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.

Source Code

gf_apply_filters( 'gform_mailchimp_field_value', array( $form['id'], $field_id ), $field_value, $form['id'], $field_id, $entry, $this->merge_var_name )

This filter is located in GFMailChimp::maybe_override_field_value() in class-gf-mailchimp.php.