-
-
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; | |
} |
This is fantastic, thank you so much! I'm shocked that something like this hasn't been implemented in Gravity Forms core yet.
Works like a charm. Thanks
I've changed line 11 to:
if ( $field->type == 'select' || $field->type == 'multiselect' ) {
to make it work for my use-case with multiselect too.
Thank you! It works like a charm!
Somehow the placeholder isn't disabled with this script anymore. So when your field is not mandatory, you should do some validation yourself.
Where is this code placed to enable the optgroup option?
I just ran into this today and found it quite useful. I noticed that it didn't seem to support ungrouped options at the beginning so I took a stab at my own version: https://gist.github.com/aaemnnosttv/72d0d34fad21d3f906ac9f3bdd61bd50
I just ran into this today and found it quite useful. I noticed that it didn't seem to support ungrouped options at the beginning so I took a stab at my own version: https://gist.github.com/aaemnnosttv/72d0d34fad21d3f906ac9f3bdd61bd50
Yes, it messes things up when 1st option isnt grp option
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
This worked great. Thanks