-
Notifications
You must be signed in to change notification settings - Fork 92
gw-default-choice-merge-tags-values.php: Added new snippet to set choice-based merge tags to return values by default.
#1189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
…hoice-based merge tags to return values by default.
WalkthroughAdds 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 Changes
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
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Suggested reviewers
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this 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
📒 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 ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
Verify completeness of choice-based field type list.
The field type list includes option and several add-on types. Please verify:
- Whether
optionis a valid standalone input type (it may only exist as part of product fields) - 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:
- Drop Down (select). [1]
- Multi-Select (multi-select dropdown). [1]
- Radio Buttons. [1]
- Checkboxes. [1]
- Multiple Choice (single/multi — combines radio/checkbox behavior). [1]
- 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 fieldimage_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}' ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 appendvalue}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.
| $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.
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