Skip to content

Commit 686a386

Browse files
authored
gw-default-choice-merge-tags-values.php: Added new snippet to set choice-based merge tags to return values by default.
1 parent 80fa6c4 commit 686a386

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* Gravity Wiz // Gravity Forms // Default Choice Merge Tags to Values
4+
* https://gravitywiz.com
5+
*
6+
* Forces choice-based merge tags to return values unless a modifier is already set.
7+
*/
8+
add_filter( 'gform_pre_replace_merge_tags', function ( $text, $form, $entry, $url_encode, $esc_html, $nl2br, $format ) {
9+
10+
// Set specific form IDs to target, or leave empty for all forms.
11+
$target_form_ids = array(); // e.g. array( 1, 3, 5 )
12+
13+
if ( $target_form_ids && ! in_array( (int) rgar( $form, 'id' ), $target_form_ids, true ) ) {
14+
return $text;
15+
}
16+
17+
preg_match_all( '/{[^{}]*?:(\d+(?:\.\d+)?)(?::([^}]*))?}/', $text, $matches, PREG_SET_ORDER );
18+
19+
foreach ( $matches as $match ) {
20+
$field_id = $match[1];
21+
$field = GFAPI::get_field( $form, $field_id );
22+
23+
if ( ! $field || ! in_array( $field->get_input_type(), array( 'select', 'radio', 'checkbox', 'multiselect', 'option', 'product', 'poll', 'survey', 'quiz', 'post_category' ), true ) ) {
24+
continue;
25+
}
26+
27+
$modifiers = isset( $match[2] ) && $match[2] !== '' ? array_map( 'trim', array_map( 'strtolower', explode( ',', $match[2] ) ) ) : array();
28+
29+
// If any modifier is already present, honor it.
30+
if ( $modifiers ) {
31+
continue;
32+
}
33+
34+
$replacement = rtrim( $match[0], '}' ) . ( $match[2] === '' ? ':value}' : ',value}' );
35+
$text = str_replace( $match[0], $replacement, $text );
36+
}
37+
38+
return $text;
39+
}, 9, 7 );

0 commit comments

Comments
 (0)