Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * 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.
- * Optionally end the group by setting the option value to `optgroupend`. 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";
- $opt_group_regex_all = "/<\s*?option\s*?value='optgroup\b[^>]*>[^<>]+<\/option\b[^>]*>(.*?)<\s*?option\s*?value='optgroupend\b[^>]*>[^<>]+<\/option\b[^>]*>/i";
- $opt_group_regex_end = "/<\s*?option\s*?value='optgroupend\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 ){
- $split_options_end = preg_split($opt_group_regex_end, $option);
- if( count($split_options_end) > 1 ){
- if (isset($split_options_end[1])) {
- $fixed_options .= "<optgroup label='" . $opt_group_match[1][0] . "'>" . $split_options_end[0] . '</optgroup>';
- $fixed_options .= $split_options_end[1];
- } else {
- $fixed_options .= $split_options_end[0];
- }
- } else {
- $fixed_options .= "<optgroup label='" . $opt_group_match[1][$index] . "'>" . $option . '</optgroup>';
- }
- }
- }
- $input = str_replace($opt_values[1], $fixed_options, $input);
- }
- }
- return $input;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement