Using Gravity Forms ‘gform_pre_replace_merge_tags’ PHP filter

The gform_pre_replace_merge_tags filter allows you to replace default merge tags before they are replaced by GFCommon::replace_variables().

Usage

add_filter('gform_pre_replace_merge_tags', 'replace_custom_merge_tags', 10, 7);

Parameters

  • $text (string): The current text in which merge tags are being replaced.
  • $form (Form Object): The current form.
  • $entry (Entry Object): The current entry.
  • $url_encode (boolean): Whether or not to encode any URLs found in the replaced value.
  • $esc_html (boolean): Whether or not to encode HTML found in the replaced value.
  • $nl2br (boolean): Whether or not to convert newlines to break tags.
  • $format (string): Determines how the value should be formatted. Default is html.

More information

See Gravity Forms Docs: gform_pre_replace_merge_tags

Examples

Replace {form_title} with custom text

This example demonstrates how to replace the {form_title} merge tag with your own custom text.

add_filter('gform_pre_replace_merge_tags', function($text, $form, $entry, $url_encode, $esc_html, $nl2br, $format) {
    $merge_tag = '{form_title}';
    if (strpos($text, $merge_tag) === false || empty($form)) {
        return $text;
    }
    return str_replace($merge_tag, 'Your Custom Text', $text);
}, 10, 7);

Replace {custom_merge_tag} with entry value

This example demonstrates how to create a custom merge tag {custom_merge_tag} and replace it with a specific entry value.

add_filter('gform_pre_replace_merge_tags', function($text, $form, $entry, $url_encode, $esc_html, $nl2br, $format) {
    $merge_tag = '{custom_merge_tag}';
    if (strpos($text, $merge_tag) === false || empty($entry)) {
        return $text;
    }
    return str_replace($merge_tag, $entry['1'], $text);
}, 10, 7);

Replace {total} with formatted currency

This example demonstrates how to replace the {total} merge tag with a formatted currency value.

add_filter('gform_pre_replace_merge_tags', function($text, $form, $entry, $url_encode, $esc_html, $nl2br, $format) {
    $merge_tag = '{total}';
    if (strpos($text, $merge_tag) === false || empty($entry)) {
        return $text;
    }
    $formatted_total = money_format('%i', $entry['total']);
    return str_replace($merge_tag, $formatted_total, $text);
}, 10, 7);

Replace {current_date} with today’s date

This example demonstrates how to replace the {current_date} merge tag with today’s date in a custom format.

add_filter('gform_pre_replace_merge_tags', function($text, $form, $entry, $url_encode, $esc_html, $nl2br, $format) {
    $merge_tag = '{current_date}';
    if (strpos($text, $merge_tag) === false) {
        return $text;
    }
    $date = date('F j, Y');
    return str_replace($merge_tag, $date, $text); 
}, 10, 7);