-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 ) ) { | ||||||
| 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}' ); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Critical: Fix undefined array index and incorrect replacement logic. The ternary condition
The current use of 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
Suggested change
🤖 Prompt for AI Agents |
||||||
| $text = str_replace( $match[0], $replacement, $text ); | ||||||
| } | ||||||
|
|
||||||
| return $text; | ||||||
| }, 9, 7 ); | ||||||
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
optionand several add-on types. Please verify:optionis a valid standalone input type (it may only exist as part of product fields)🌐 Web query:
💡 Result:
Core Gravity Forms choice-based fields:
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:
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_categoryfield's input type and the newmultiple_choicefield 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_categoryMissing:
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 fieldThe
optionandpost_categoryfield types are valid and correctly included.🤖 Prompt for AI Agents