-
-
Save codearachnid/a06e13be7f01b81b838c to your computer and use it in GitHub Desktop.
<?php | |
/** | |
* Filter Gravity Forms select field display to wrap optgroups where defined | |
* USE: | |
* set the value of the select option to `optgroup` within the form editor. The | |
* filter will then automagically wrap the options following until the start of | |
* the next option group | |
*/ | |
/** | |
* Filter Gravity Forms select field display to wrap optgroups where defined | |
* USE: | |
* set the value of the select option to `optgroup` within the form editor. The | |
* filter will then automagically wrap the options following until the start of | |
* the next option group | |
*/ | |
add_filter('gform_field_content', 'filter_gf_select_optgroup', 10, 2); | |
function filter_gf_select_optgroup($input, $field) { | |
if ($field->type !== 'select') | |
return $input; | |
// Extract the content between <select> tags | |
$select_regex = "/<\s*?select\b[^>]*>(.*?)<\s*?\/select\b[^>]*>/is"; | |
if (!preg_match($select_regex, $input, $select_matches)) { | |
return $input; | |
} | |
$options_content = $select_matches[1]; | |
// Pattern to match individual <option> tags | |
$option_regex = "/<\s*?option\s*?value=['\"]([^'\"]+)['\"][^>]*>(.*?)<\/option>/is"; | |
preg_match_all($option_regex, $options_content, $option_matches, PREG_SET_ORDER); | |
if (empty($option_matches)) { | |
return $input; | |
} | |
$new_content = ''; | |
$in_optgroup = false; | |
foreach ($option_matches as $option) { | |
$full_option = $option[0]; | |
$value = $option[1]; | |
$label = $option[2]; | |
if ($value === 'optgroup') { | |
if ($in_optgroup) { | |
$new_content .= '</optgroup>'; | |
} | |
$new_content .= "<optgroup label='" . htmlspecialchars($label) . "'>"; | |
$in_optgroup = true; | |
} else { | |
$new_content .= $full_option; | |
} | |
} | |
if ($in_optgroup) { | |
$new_content .= '</optgroup>'; | |
} | |
$input = str_replace($options_content, $new_content, $input); | |
return $input; | |
} |
I'm using the update proposed by @aaemnnosttv per https://gist.github.com/codearachnid/a06e13be7f01b81b838c#gistcomment-2724704 pointing out the important fact that ungrouped options at the start break things as this code is.
Exactly what I was looking for. Brilliant! Thank you much.
Is this seriously still not supported natively?
Thanks for this! Had to change the regex to expect a space after value='[value]'
, and I changed it so the "optgroup" was the label instead of the value, since the values were coming from an array in PHP, and it just replaced the optgroup
item over and over and left me with one optgroup. Otherwise, this was a huge help!
<\s*?option\s*?value='([^<>]+)'\s*?>optgroup<\/option\b[^>]*>
@SeanDKendle thank you for the update - I've taken that suggestion - also enhanced the method to work with latest versions of GravityForms
Yes, it messes things up when 1st option isnt grp option