Skip to content

Conversation

@SebastianWiz
Copy link
Contributor

Context

⛑️ Ticket(s): https://secure.helpscout.net/conversation/3140355922/93621?viewId=3808239

Summary

This PR adds a new snippet that allows the user to have their choice-based merge tags return values instead of labels, by default. Note that if the merge tag is added using a modifier, that modifier will be honored.

Example:
{Radio button:3} // Would return value
{Radio button:3:choice_label} // Would return choice label

…hoice-based merge tags to return values by default.
@coderabbitai
Copy link

coderabbitai bot commented Nov 21, 2025

Walkthrough

Adds a WordPress Gravity Forms filter hook to modify choice-based merge tags. The filter intercepts merge tags before replacement, checks field types, and rewrites tags to append :value or ,value modifiers when none are specified, ensuring choice fields return underlying values by default.

Changes

Cohort / File(s) Summary
Gravity Forms merge tag filter
gravity-forms/gw-default-choice-merge-tags-values.php
Introduces gform_pre_replace_merge_tags filter implementation that parses merge tags, validates choice-based field types, and rewrites tags to append value modifiers when no modifiers are present. Hook runs at priority 9 and targets all forms by default with optional form ID filtering.

Sequence Diagram(s)

sequenceDiagram
    participant GF as Gravity Forms
    participant Filter as gform_pre_replace_merge_tags<br/>Hook (Priority 9)
    participant Parser as Merge Tag Parser
    participant Validator as Field Validator
    participant Rewriter as Tag Rewriter

    GF->>Filter: pre_replace_merge_tags event
    Filter->>Parser: parse merge tags from text
    Parser->>Validator: extract field ID & check field type
    alt Field is choice-based
        Validator->>Rewriter: field type valid
        alt No modifiers present
            Rewriter->>Rewriter: append :value or ,value modifier
            Rewriter->>Filter: return modified text
        else Modifiers exist
            Rewriter->>Filter: return unchanged text
        end
    else Field type not choice-based
        Validator->>Filter: return unchanged text
    end
    Filter->>GF: return processed text
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Review the merge tag parsing logic and regex/string manipulation patterns
  • Validate the choice-field type detection logic against supported Gravity Forms field types
  • Verify the conditional logic for modifier detection and tag rewriting
  • Check hook priority (9) appropriateness and parameter handling (7 parameters mentioned)

Suggested reviewers

  • veryspry

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: adding a new snippet that sets choice-based merge tags to return values by default.
Description check ✅ Passed The description includes the required Context and Summary sections with a relevant Help Scout ticket link and clear examples demonstrating the new functionality.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch SebastianWiz-patch-1

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link

Warnings
⚠️ When ready, don't forget to request reviews on this pull request from your fellow wizards.
Messages
📖 Merlin would give this scroll the highest of praises. Cheers for adding this new snippet to the library! 🪄

Generated by 🚫 dangerJS against 686a386

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 80fa6c4 and 686a386.

📒 Files selected for processing (1)
  • gravity-forms/gw-default-choice-merge-tags-values.php (1 hunks)
🔇 Additional comments (1)
gravity-forms/gw-default-choice-merge-tags-values.php (1)

17-17: Regex pattern is correct for field-based merge tags.

The pattern correctly matches only field merge tags ({Label:FieldID} and {Label:FieldID:modifiers}) with numeric field IDs including subfield decimals (e.g., 1.2). Gravity Forms supports various merge tag formats including basic/global tags like {form_id}, field tags like {Label:ID}, and custom add-on tags, but the regex appropriately excludes these non-field formats since the filter is designed to target only choice-based field merge tags, not system or custom tags.

No changes needed to the regex pattern.

$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;
}

$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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants