gform_pre_replace_merge_tags

Description

The gform_pre_replace_merge_tags filter allows developers 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

ParameterTypeDescription
$textstringThe current text in which merge tags are being replaced.
$formForm ObjectThe current form.
$entryEntry ObjectThe current entry.
$url_encodebooleanWhether or not to encode any URLs found in the replaced value.
$esc_htmlbooleanWhether or not to encode HTML found in the replaced value.
$nl2brbooleanWhether or not to convert newlines to break tags.
$formatstringDetermines how the value should be formatted. Default is html.

Examples

1. Replace {form_title}.

This example shows how you can 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 );

2. Add a {days_remaining} merge tag.

This example adds a {days_remaining} merge tag that counts down the days left in the current calendar month.

add_filter( 'gform_pre_replace_merge_tags', function ( $text, $form, $entry, $url_encode, $esc_html, $nl2br, $format ) {
	$merge_tag = '{days_remaining}';

	if ( strpos( $text, $merge_tag ) === false ) {
		return $text;
	}

	$today     = new DateTime( 'today' );
	$month_end = new DateTime( 'last day of this month' );
	$days_left = (int) $today->diff( $month_end )->format( '%a' );

	return str_replace( $merge_tag, $days_left, $text );
}, 10, 7 );

3. Add a {random_order_number} merge tag.

This example adds a {random_order_number} merge tag that generates a unique reference number for use in confirmation messages and notifications.

add_filter( 'gform_pre_replace_merge_tags', function ( $text, $form, $entry, $url_encode, $esc_html, $nl2br, $format ) {
	$merge_tag = '{random_order_number}';

	if ( strpos( $text, $merge_tag ) === false ) {
		return $text;
	}

	$order_number = 'ORD-' . strtoupper( wp_generate_password( 8, false ) );

	return str_replace( $merge_tag, $order_number, $text );
}, 10, 7 );

4. Add a {business_hours_status} merge tag.

This example adds a {business_hours_status} merge tag that outputs whether the submission was made during business hours (9am–5pm, Monday–Friday).

add_filter( 'gform_pre_replace_merge_tags', function ( $text, $form, $entry, $url_encode, $esc_html, $nl2br, $format ) {
	$merge_tag = '{business_hours_status}';

	if ( strpos( $text, $merge_tag ) === false ) {
		return $text;
	}

	$now       = current_time( 'timestamp' );
	$day       = (int) date( 'N', $now );
	$hour      = (int) date( 'G', $now );
	$is_open   = $day <= 5 && $hour >= 9 && $hour < 17;
	$status    = $is_open ? 'Received during business hours' : 'Received after business hours';

	return str_replace( $merge_tag, $status, $text );
}, 10, 7 );

Placement

This code can be used in the functions.php file of the active theme, a custom functions plugin, a custom add-on, or with a code snippets plugin.

See also the PHP section in this article: Where Do I Put This Code?

Source Code

apply_filters( 'gform_pre_replace_merge_tags', $text, $form, $entry, $url_encode, $esc_html, $nl2br, $format )

This filter is located in GFCommon::replace_variables() in common.php.

Since

This filter was added in Gravity Forms 1.9.6.