Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add the optgroup ability to Gravity Forms default select field.
/**
* 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' ) {
$opt_placeholder_regex = strpos($input,'gf_placeholder') === false ? '' : "<\s*?option.*?class='gf_placeholder'>[^<>]+<\/option\b[^>]*>";
$opt_regex = "/<\s*?select\b[^>]*>" . $opt_placeholder_regex . "(.*?)<\/select\b[^>]*>/i";
$opt_group_regex = "/<\s*?option\s*?value='optgroup\b[^>]*>([^<>]+)<\/option\b[^>]*>/i";
preg_match($opt_regex, $input, $opt_values);
$split_options = preg_split($opt_group_regex, $opt_values[1]);
$optgroup_found = count($split_options) > 1;
// sometimes first item in the split is blank
if( strlen($split_options[0]) < 1 ){
unset($split_options[0]);
$split_options = array_values( $split_options );
}
if( $optgroup_found ){
$fixed_options = '';
preg_match_all($opt_group_regex, $opt_values[1], $opt_group_match);
if( count($opt_group_match) > 1 ){
foreach( $split_options as $index => $option ){
$fixed_options .= "<optgroup label='" . $opt_group_match[1][$index] . "'>" . $option . '</optgroup>';
}
}
$input = str_replace($opt_values[1], $fixed_options, $input);
}
}
return $input;
}
@stevenmunro

This comment has been minimized.

Copy link

@stevenmunro stevenmunro commented Aug 31, 2015

I don't know how long you have been working on this, but perfect! I had my own implementation which worked, but unfortunately became broken after field validation. So tried yours and it is all good.

@Critter

This comment has been minimized.

Copy link

@Critter Critter commented Aug 17, 2016

This worked great. Thanks

@johnolek

This comment has been minimized.

Copy link

@johnolek johnolek commented Sep 16, 2016

This is fantastic, thank you so much! I'm shocked that something like this hasn't been implemented in Gravity Forms core yet.

@YourMark

This comment has been minimized.

Copy link

@YourMark YourMark commented Nov 9, 2016

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.

@akkis

This comment has been minimized.

Copy link

@akkis akkis commented Nov 16, 2016

Thank you! It works like a charm!

@wgroenewold

This comment has been minimized.

Copy link

@wgroenewold wgroenewold commented Jan 27, 2017

Somehow the placeholder isn't disabled with this script anymore. So when your field is not mandatory, you should do some validation yourself.

@daela

This comment has been minimized.

Copy link

@daela daela commented Mar 25, 2017

Where is this code placed to enable the optgroup option?

@aaemnnosttv

This comment has been minimized.

Copy link

@aaemnnosttv aaemnnosttv commented Oct 5, 2018

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

@ztobs

This comment has been minimized.

Copy link

@ztobs ztobs commented May 24, 2019

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

@KZeni

This comment has been minimized.

Copy link

@KZeni KZeni commented Sep 11, 2019

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.

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