I’ve put a solution to this problem in place, you may think of a better one:
$html = "<select class='rwmb-select'{$name}{$id}{$disabled}>";
$current_optgroup = "";
foreach ( $field['options'] as $key => $value )
{
if( $value == "optgroup" )
{
if( ($current_optgroup != $key) && (!empty($current_optgroup)) )
{
//vmodha: If there was a previously opened optgroup then close it before opening a new one.
$html .= '</optgroup>';
}
$current_optgroup = $key;
$html .= '<optgroup label="' . $key . '">';
continue;
}
$selected = selected( in_array( $key, $meta ), true, false );
$html .= "<option value='{$key}'{$selected}>{$value}</option>";
}
if( !empty($current_optgroup) )
{
$html .= '</optgroup>';
}
$html .= "</select>";
You simply need to provide the optgroup title in the array KEY and set the VALUE of the array as ‘optgroup’.
'Brands And Stuff' => 'optgroup',
'Brands' => 'Brands',
'Agency Partners' => 'Agency Partners',
'Second Group' => 'optgroup',
'Production Partners' => 'Production Partners',
'DMS' => 'DMS'
Simple but you may think up something better.
Cheers
vmodha