Add Gravity Forms’ Custom Merge Tag For Day, Month and Year

WordPress Gravity Forms plugin lets you set dynamic values for fields via shortcode ( merge tag ). You can populate a field with a date value using the merge tag {date_mdy}, {date_dmy} etc. These tags Display the current date in the MM/DD/YYYY or DD/MM/YYYY format. But there is no tag for individual parts of date like day, month and year.
Use the code snippet below. It will allow you to use 3 custom merge tags .

  • {date_d} – to show the day number.
  • {date_m} – to show the month number.
  • {date_y} – to show the year number.
<?php

/**
 * CUSTOM MERGE TAGS FOR GRAVITY FORMS
 * {date_m}
 * {date_d}
 * {date_y}
 *
 * Author : oneTarek
 * http://onetarek.com
 */

add_filter('gform_replace_merge_tags', 'otk_gform_replace_merge_tags', 10, 7 );
function otk_gform_replace_merge_tags( $text, $form, $entry, $url_encode, $esc_html, $nl2br, $format ) {
	if ( strpos( $text, '{' ) !== false ) {
		//date (mm/dd/yyyy)
		$local_timestamp = GFCommon::get_local_timestamp( time() );
		$local_date_m = date_i18n( 'm', $local_timestamp, true );
		$local_date_d = date_i18n( 'd', $local_timestamp, true );
		$local_date_y = date_i18n( 'Y', $local_timestamp, true );
		$text = str_replace( '{date_m}', $url_encode ? urlencode( $local_date_m ) : $local_date_m, $text );
		$text = str_replace( '{date_d}', $url_encode ? urlencode( $local_date_d ) : $local_date_d, $text );
		$text = str_replace( '{date_y}', $url_encode ? urlencode( $local_date_y ) : $local_date_y, $text );
	}
	return $text;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.