Skip to content

Instantly share code, notes, and snippets.

@codearachnid
Last active March 24, 2025 15:39
Show Gist options
  • Save codearachnid/a06e13be7f01b81b838c to your computer and use it in GitHub Desktop.
Save codearachnid/a06e13be7f01b81b838c to your computer and use it in GitHub Desktop.
Add the optgroup ability to Gravity Forms default select field.
<?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;
}
@codearachnid
Copy link
Author

@SeanDKendle thank you for the update - I've taken that suggestion - also enhanced the method to work with latest versions of GravityForms

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