Setting Default Values For MB Builder Custom Field Types
Support › MB Builder › Setting Default Values For MB Builder Custom Field TypesResolved
- This topic has 3 replies, 3 voices, and was last updated 2 years, 1 month ago by
Sam.
-
AuthorPosts
-
February 14, 2023 at 3:28 AM #40506
James Cooper
ParticipantI 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 MB 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 MB Builder?
Best Regards,
Ryan
February 14, 2023 at 10:44 PM #40517Peter
ModeratorHello,
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 settingstd
.February 15, 2023 at 12:26 AM #40525James Cooper
ParticipantI 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 MB 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', ], ];
February 21, 2023 at 8:31 PM #40641Sam
ParticipantGreat 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
-
AuthorPosts
- You must be logged in to reply to this topic.