Skip to content

Instantly share code, notes, and snippets.

@saifsultanc
Last active February 2, 2023 17:53
Show Gist options
  • Save saifsultanc/236b422636f9e73ed30dee318520598c to your computer and use it in GitHub Desktop.
Save saifsultanc/236b422636f9e73ed30dee318520598c to your computer and use it in GitHub Desktop.
gw-date-merge-tags-for-non-date-field
add_filter( 'gform_pre_replace_merge_tags', function( $text, $form, $entry, $url_encode, $esc_html, $nl2br, $format ) {
preg_match_all( '/{[^{]*?:(\d+(\.\d+)?)(:(.*?))?}/mi', $text, $matches, PREG_SET_ORDER );
foreach ( $matches as $match ) {
$input_id = $match[1];
$field = GFFormsModel::get_field( $form, $input_id );
if ( ! $field ) {
continue;
}
$i = $match[0][0] === '{' ? 4 : 5;
$modifier = rgar( array_map( 'trim', explode( ',', rgar( $match, $i ) ) ), 0 );
if ( ! $modifier ) {
continue;
}
$value = GFFormsModel::get_lead_field_value( $entry, $field );
$value = $field->get_value_merge_tag( $value, $input_id, $entry, $form, $modifier, $value, $url_encode, $esc_html, $format, $nl2br );
if ( ! $value ) {
continue;
}
$value = substr( $value, 0, -9 ); // for date line text prepare only yyyy-mm-dd format
$format = $field->dateFormat ? $field->dateFormat : 'ymd_dash';
$parsed_date = GFCommon::parse_date( $value, $format );
// phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
$replace = date( $modifier, strtotime( sprintf( '%d-%d-%d', $parsed_date['year'], $parsed_date['month'], $parsed_date['day'] ) ) );
$text = str_replace( $match[0], $replace, $text );
}
return $text;
}, 10, 7 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment