Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions gravity-forms/gw-default-choice-merge-tags-values.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* Gravity Wiz // Gravity Forms // Default Choice Merge Tags to Values
* https://gravitywiz.com
*
* Forces choice-based merge tags to return values unless a modifier is already set.
*/
add_filter( 'gform_pre_replace_merge_tags', function ( $text, $form, $entry, $url_encode, $esc_html, $nl2br, $format ) {

// Set specific form IDs to target, or leave empty for all forms.
$target_form_ids = array(); // e.g. array( 1, 3, 5 )

if ( $target_form_ids && ! in_array( (int) rgar( $form, 'id' ), $target_form_ids, true ) ) {
return $text;
}

preg_match_all( '/{[^{}]*?:(\d+(?:\.\d+)?)(?::([^}]*))?}/', $text, $matches, PREG_SET_ORDER );

foreach ( $matches as $match ) {
$field_id = $match[1];
$field = GFAPI::get_field( $form, $field_id );

if ( ! $field || ! in_array( $field->get_input_type(), array( 'select', 'radio', 'checkbox', 'multiselect', 'option', 'product', 'poll', 'survey', 'quiz', 'post_category' ), true ) ) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

Verify completeness of choice-based field type list.

The field type list includes option and several add-on types. Please verify:

  1. Whether option is a valid standalone input type (it may only exist as part of product fields)
  2. Whether any other choice-based field types from Gravity Forms or popular add-ons are missing

🌐 Web query:

What are all the choice-based field input types in Gravity Forms including add-ons?

💡 Result:

Core Gravity Forms choice-based fields:

  1. Drop Down (select). [1]
  2. Multi-Select (multi-select dropdown). [1]
  3. Radio Buttons. [1]
  4. Checkboxes. [1]
  5. Multiple Choice (single/multi — combines radio/checkbox behavior). [1]
  6. Product / Option fields (product options with choices). [2]

Choice-based fields provided by official Gravity Forms add-ons:
7. Quiz field (choices as Radio / Checkboxes / Drop Down). [3]
8. Poll field (choices as Radio / Checkboxes / Drop Down). [4]
9. Survey field types (Survey add‑on: Likert, Rating/Scale/Opinion items, NPS and survey question fields that use radio/checkbox/dropdown choices). [5]

Common third‑party (certified) add-ons that add or extend choice inputs:
10. Image Choices — image-based choices for Radio/Checkboxes (Gravity Forms official add‑on). [6]
11. List Dropdown — adds dropdown/select choices inside List field columns. [7]
12. Limit Choices — per‑choice selection limits for Radio/Checkbox/Drop Down/Multi‑Select (Gravity Wiz / certified add‑on). [8]
13. Randomizer / Gravity Perks — randomize choice order for any choice‑based field (supports Drop Down, Multi‑Select, Radio, Checkboxes, Products, Options). [9]

Notes:

  • Many choice fields share the same presentation options (radio, checkbox, dropdown/multiselect) and are exposed by several add‑ons (Quiz, Poll, Survey, Products/Options). See Gravity Forms docs for each field/add‑on for full configuration options. [1][3][4][5]

Sources:
[1] Gravity Forms — Edit Choices / choice fields documentation.
[2] Gravity Forms — Option field (product/options).
[3] Gravity Forms — Quiz field (Quiz Add‑On).
[4] Gravity Forms — Poll field (Polls Add‑On).
[5] Gravity Forms — Survey-related fields (Survey Add‑On referenced in docs).
[6] Gravity Forms — Image Choices add‑on.
[7] Gravity Forms — List Dropdown add‑on.
[8] Gravity Wiz — Limit Choices (certified add‑on).
[9] Gravity Wiz / Gravity Perks — Randomizer (choice randomization).


I need to gather additional information to complete the verification. Let me search for more details about the specific field types mentioned.

Let me search for more specific information about the post_category field's input type and the new multiple_choice field type.

Add missing choice-based field types from Gravity Forms 2.9.

Gravity Forms 2.9 introduced two new choice-based field types: the Multiple Choice field and the Image Choice field. These should be added to the field type list:

Current list: select, radio, checkbox, multiselect, option, product, poll, survey, quiz, post_category

Missing:

  • multiple_choice - combines the functionality of the checkbox and radio button field types into one enhanced field
  • image_choice - allows you to display images as the choices for a single or multi-select field

The option and post_category field types are valid and correctly included.

🤖 Prompt for AI Agents
In gravity-forms/gw-default-choice-merge-tags-values.php around line 23, the
array of choice-based Gravity Forms field input types is missing newer types
introduced in GF 2.9; update the in_array list to also include 'multiple_choice'
and 'image_choice' so those fields are treated as choice-based (i.e., add
'multiple_choice' and 'image_choice' to the array alongside the existing types).

continue;
}

$modifiers = isset( $match[2] ) && $match[2] !== '' ? array_map( 'trim', array_map( 'strtolower', explode( ',', $match[2] ) ) ) : array();

// If any modifier is already present, honor it.
if ( $modifiers ) {
continue;
}

$replacement = rtrim( $match[0], '}' ) . ( $match[2] === '' ? ':value}' : ',value}' );
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Critical: Fix undefined array index and incorrect replacement logic.

The ternary condition $match[2] === '' will trigger an undefined array index warning when $match[2] is not set (i.e., when the original merge tag has no modifiers section at all, like {Field:1}). Additionally, the logic is incorrect:

  • When $match[2] is not set (tag is {Field:1}): should append :value} to get {Field:1:value}
  • When $match[2] is set but empty (tag is {Field:1:}): should append value} to get {Field:1:value}

The current use of ,value} is incorrect—commas separate multiple modifiers, not introduce the first one.

Apply this diff to fix both issues:

-		$replacement = rtrim( $match[0], '}' ) . ( $match[2] === '' ? ':value}' : ',value}' );
+		$replacement = rtrim( $match[0], '}' ) . ( isset( $match[2] ) ? 'value}' : ':value}' );
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
$replacement = rtrim( $match[0], '}' ) . ( $match[2] === '' ? ':value}' : ',value}' );
$replacement = rtrim( $match[0], '}' ) . ( isset( $match[2] ) ? 'value}' : ':value}' );
🤖 Prompt for AI Agents
In gravity-forms/gw-default-choice-merge-tags-values.php around line 34, the
current ternary accesses $match[2] directly causing undefined index warnings and
uses the wrong separator; change the logic to check isset($match[2]) instead of
comparing to '' and append 'value}' when the modifiers part exists (even if
empty) or ':value}' when it does not exist, so the replacement uses ':' only
when adding the first modifier and avoids undefined index errors.

$text = str_replace( $match[0], $replacement, $text );
}

return $text;
}, 9, 7 );