Callback function with optgroup
Support › MB Custom Post Type › Callback function with optgroup
- This topic has 5 replies, 2 voices, and was last updated 2 years, 1 month ago by
Peter.
-
AuthorPosts
-
September 13, 2023 at 4:37 PM #43237
Laurent Brugiere
ParticipantHi,
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
Model4September 15, 2023 at 10:57 PM #43255Peter
ModeratorHello,
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' ]; }September 19, 2023 at 3:12 PM #43275Laurent Brugiere
ParticipantSorry, 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
September 19, 2023 at 8:22 PM #43276Peter
ModeratorHello,
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
flattentofalse. Follow the documentation https://docs.metabox.io/fields/select/#settingsSeptember 19, 2023 at 11:34 PM #43282Laurent Brugiere
ParticipantThanks.
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.September 20, 2023 at 10:17 PM #43291Peter
ModeratorHello,
You can set the
flattensetting 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 -
AuthorPosts
- You must be logged in to reply to this topic.