Support Forum
Support › MB Custom Post Type › Callback function with optgroup
Hi,
Is it possible to return an array with optgroup in a callback function for an advanced_select?
I would like a select like this:
BrandA
Model1
Model2
BrandB
Model3
Model4
Hello,
Yes, it is possible. You can define a custom function in the file functions.php before adding the callback function in the MB builder. The choice should have the format: value => Label
. For example:
function my_custom_callback() {
return [
'branda' => 'BrandA',
'model1' => 'Model1',
'model2' => 'Model2'
];
}
Sorry, I tested your code, but it creates options only.
Brand becomes an option instead of a label for an optgroup.
I tested with array for each brand, but it did not work.
What I want is like this https://codepen.io/doitian/pen/nKBbmz
Hello,
For the option group, it isn't possible. But you can try to set the sub-choices like this
function my_custom_callback() {
return [
[
'value' => 'monkeys',
'label' => 'Monkeys',
],
[
'value' => 'king_kong',
'label' => 'King Kong',
'parent' => 'monkeys',
],
[
'value' => 'curious_george',
'label' => 'Curious George',
'parent' => 'monkeys',
],
[
'value' => 'donkeys',
'label' => 'Donkeys',
],
[
'value' => 'eeyore',
'label' => 'Eeyore',
'parent' => 'donkeys',
],
[
'value' => 'guss',
'label' => 'Gus',
'parent' => 'donkeys',
],
];
}
and set the setting flatten
to false
. Follow the documentation https://docs.metabox.io/fields/select/#settings
Thanks.
But flatten does not exist in the UI.
I tested with adding an option flatten => false from UI.
But it does not work.
Metabox is a nightmare for me.
Nothing is well explained in the doc.
I switched to ACF, and do the job in one hour instead of debugging the code for one month.
I lost $49.
Hello,
You can set the flatten
setting by using the custom settings of the field. Please check this screenshot https://imgur.com/kiiEgca
It is noted in the documentation https://docs.metabox.io/extensions/meta-box-builder/#custom-settings