The gform_replace_merge_tags filter allows you to replace custom merge tags in Gravity Forms.
Usage
add_filter('gform_replace_merge_tags', 'your_custom_function', 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 ishtml.
More information
See Gravity Forms Docs: gform_replace_merge_tags
Examples
Replace a custom merge tag
Replace a custom merge tag with the corresponding value.
add_filter('gform_replace_merge_tags', 'replace_download_link', 10, 7);
function replace_download_link($text, $form, $entry, $url_encode, $esc_html, $nl2br, $format) {
$custom_merge_tag = '{download_link}';
if (strpos($text, $custom_merge_tag) === false) {
return $text;
}
$download_link = gform_get_meta($entry['id'], 'gfmergedoc_download_link');
$text = str_replace($custom_merge_tag, $download_link, $text);
return $text;
}
Replace {entry_time}
Replace a custom merge tag with the entry date and time.
add_filter('gform_replace_merge_tags', function ($text, $form, $entry, $url_encode, $esc_html, $nl2br, $format) {
$merge_tag = '{entry_time}';
if (strpos($text, $merge_tag) === false || empty($entry) || empty($form)) {
return $text;
}
return str_replace($merge_tag, GFCommon::format_date(rgar($entry, 'date_created'), false, 'Y/m/d'), $text);
}, 10, 7);
Replace {transaction_id}
Replace a custom merge tag with the payment transaction id.
add_filter('gform_replace_merge_tags', function ($text, $form, $entry, $url_encode) {
$merge_tag = '{transaction_id}';
if (strpos($text, $merge_tag) === false || empty($form) || empty($entry)) {
return $text;
}
$transaction_id = esc_html(rgar($entry, 'transaction_id'));
return str_replace($merge_tag, $url_encode ? urlencode($transaction_id) : $transaction_id, $text);
}, 10, 4);