Using Gravity Forms ‘gform_merge_tag_filter’ PHP action

The gform_merge_tag_filter is a Gravity Forms PHP filter that allows you to dynamically change the merge tag output. It’s helpful when filtering field types from being displayed in the {all_fields} merge tag, or when implementing custom field types that require the merge tag output to be different from the value stored with the entry.

Usage

A generic example of using the filter:

add_filter('gform_merge_tag_filter', 'filter_merge_tag', 10, 6);

Parameters

  • $value (string): The current merge tag value to be filtered. Replace it with any other text to replace the merge tag output, or return “false” to disable this field’s merge tag output.
  • $merge_tag (string): If the merge tag being executed is an individual field merge tag (e.g. {Name:3}), this variable will contain the field’s ID. If not, this variable will contain the name of the merge tag (e.g. all_fields).
  • $modifier (string): The string containing any modifiers for this merge tag. For example, “value,nohidden” would be the modifiers for the following merge tag: {all_fields:value,nohidden}.
  • $field (Field Object): The current field.
  • $raw_value (Mixed): The raw value submitted for this field.
  • $format (string): Whether the text is formatted as html or text.

More information

See Gravity Forms Docs: gform_merge_tag_filter

Examples

Exclude fields

This example excludes any hidden fields from the {all_fields} merge tag and adds a placeholder text for the field whose ID is 2.

add_filter('gform_merge_tag_filter', 'filter_all_fields', 10, 6);
function filter_all_fields($value, $merge_tag, $modifier, $field, $raw_value, $format) {
    if ($merge_tag == 'all_fields' && $field->type == 'hidden') {
        return false;
    } elseif ($merge_tag == 'all_fields' && $field->id == '2') {
        return 'Call us for details';
    } else {
        return $value;
    }
}

Add an Address field modifier

This example adds a new modifier to the address field which replaces the field value with a map link. {Address:15:map_link}

add_filter('gform_merge_tag_filter', 'address_map_link', 11, 6);
function address_map_link($value, $merge_tag, $modifier, $field, $raw_value, $format) {
    if ($field->type == 'address' && $merge_tag != 'all_fields' && $modifier == 'map_link' && !empty($raw_value)) {
        $address_qs = GFCommon::implode_non_blank(' ', $raw_value);
        $address_qs = urlencode($address_qs);
        $value = "<a href='http://maps.google.com/maps?q={$address_qs}' target='_blank' class='map-it-link'>Map It</a>";
    }
    return $value;
}