Support Forum
Support › Meta Box Builder › How to Make Star Rating selectable in the Custom Field Builder
Hello
I have been looking at
https://docs.metabox.io/creating-new-field-types/
https://docs.metabox.io/tutorials/add-star-rating-fields/
I find it hard to implement
I have downloaded the source code
I have put this in my code snippet
add_filter( 'rwmb_meta_boxes', 'your_prefix_register_meta_boxes' );
function your_prefix_register_meta_boxes( $meta_boxes ) {
$prefix = '';
$meta_boxes[] = [
'title' => esc_html__( 'APK Fields', 'text-domain' ),
'id' => 'apk-fields',
'post_types' => ['apk'],
'context' => 'normal',
'priority' => 'high',
'fields' => [
[
'id' => $prefix . 'rating',
'type' => 'rating',
'name' => esc_html__( 'Rating', 'text-domain' ),
],
],
];
return $meta_boxes;
}
What should i put in the title above ? is it the field group id ?
What should i put in the post type ? My custom post type is APK with slug apk
I cannot see the Rating field on the builder. Read this but could not relate what are the values i must replace
https://docs.metabox.io/extensions/meta-box-builder/#adding-your-own-field-types/
What should i replace the word icon with ?
use MBB\Control;
add_filter( 'mbb_field_types', function ( $field_types ) {
$field_types['icon'] = [
'title' => __( 'Icon', 'your-text-domain' ),
'category' => 'advanced',
'controls' => [
'name', 'id', 'type', 'label_description', 'desc',
Control::Select( 'icon_type', [
'label' => __( 'Icon type', 'your-text-domain' ),
'options' => [
'dashicons' => __( 'Dashicons', 'your-text-domain' ),
'fontawesome' => __( 'Font Awesome', 'your-text-domain' ),
'url' => __( 'Custom URL', 'your-text-domain' ),
],
], 'dashicons' ),
Control::Icon( 'icon', [
'label' => __( 'Icon', 'your-text-domain' ),
'dependency' => 'icon_type:dashicons',
] ),
Control::Input( 'icon_fa', [
'label' => '<a href="https://fontawesome.com/icons?d=gallery&m=free" target="_blank" rel="noopenner noreferrer">' . __( 'FontAwesome icon class', 'your-text-domain' ) . '</a>',
'dependency' => 'icon_type:fontawesome',
] ),
Control::Input( 'icon_url', [
'label' => __( 'Icon URL', 'your-text-domain' ),
'dependency' => 'icon_type:url',
] ),
'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;
} );
Hello,
Actually, the custom star rating field above is a radio
field with options and some CSS code to display the star rating style. No need to create a custom field type in the builder.
If you want to register custom fields with code, please read more in the documentation https://docs.metabox.io/creating-fields-with-code/
Hello Peter
Would you mind guiding me slowly
I still do not get it how can i display it in front end. This is the custom field I created.
On the custom post single I set a rating for example 4.5
Now what do i need to put in Metabox View so i can see all the radio buttons at front end ?
I tried metabox view
I can only see 4.5 in the front end instead of radio buttons
Thank you
joie
I now try this in metabox view with twig syntax
{% set rating_settings = mb.rwmb_get_field_settings( 'apk_rating') %}
{% set options = rating_settings['options'] %}
<fieldset id='apk_rating'>
{% for option in options %}
<input type="radio"/> {{ option }}
{% endfor %}
</fieldset>
Could you please help me with syntax on how to set it selected ?
Thank you
Joie
Hello,
Please follow step 5 in the documentation to output the star rating https://docs.metabox.io/tutorials/add-star-rating-fields/#5-displaying-the-value-from-the-star-rating-fields
If you are not able to complete the task, please contact us here https://metabox.io/contact/
our development team will help you with an extra fee.