Support Forum
Support › Meta Box Builder › Setting Default Values For Meta Box Builder Custom Field TypesResolved
I was able to setup a star rating field using your tutorial here: https://docs.metabox.io/tutorials/add-star-rating-fields/
I was then able to add the star rating field type to the Meta Box Builder using your documentation here: https://docs.metabox.io/extensions/meta-box-builder/#extending-the-builder
add_filter( 'mbb_field_types', function ( $field_types ) {
// Add star rating field type to Meta-Box Builder
$field_types['star_rating'] = [
'title' => __( 'Star Rating', 'blogcraftco-theme' ),
'category' => 'advanced',
'controls' => [
'name', 'id', 'type', 'label_description', 'desc',
'clone', 'sort_clone', 'clone_default', 'clone_as_multiple', 'max_clone', 'add_button',
'before', 'after', 'class', 'save_field', 'sanitize_callback', 'attributes', 'custom_settings',
],
];
return $field_types;
} );
The star rating field works as expected when inserting it into any newly created field group.
The one thing that I cannot seem to find is how to have the star rating be automatically set to a default value (i.e., 5) whenever the field group is initially loaded.
Where/how would I do that for custom field types that have been added to the Meta Box Builder?
Best Regards,
Ryan
Hello,
Currently, there is no option to set the default value when adding a new field in the builder. I will inform the development team to consider supporting this in future updates.
If you want to set the default value for the field when editing the post, please use the field setting std
.
I figured it out. I just added 'std' to the list of controls for the 'star_rating' field type (which activated the Default Value field in the Meta Box Builder for the 'star_rating' field. I then set the Default Value to 5. Now, when I create a new review post, the rating is automatically set to 5 stars.
// Add star rating field type to Meta-Box Builder
$field_types['star_rating'] = [
'title' => __( 'Star Rating', 'blogcraftco-theme' ),
'category' => 'advanced',
'controls' => [
'name', 'id', 'type', 'label_description', 'desc', 'std',
'clone', 'sort_clone', 'clone_default', 'clone_as_multiple', 'max_clone', 'add_button',
'before', 'after', 'class', 'save_field', 'sanitize_callback', 'attributes', 'custom_settings',
],
];
Great thread, James (and Peter). Hopefully Metabox comes to support this natively in the near future, as this is a standard expectation of inputs nowadays.
Sam