MB Block with switch or radio buttons
- This topic has 2 replies, 2 voices, and was last updated 2 years, 9 months ago by
metafan.
Viewing 3 posts - 1 through 3 (of 3 total)
-
AuthorPosts
-
June 27, 2022 at 6:57 PM #36682
metafan
ParticipantHi,
I have some custom blocks, which work very well with text, textarea, image upload etc. even in groups.But I can't get to work the switch element or the radio element.
Any ideas?
Thanks!add_filter( 'rwmb_meta_boxes', 'custom_blocks_test' ); function custom_blocks_test( $meta_boxes ) { $prefix = ''; $meta_boxes[] = [ 'title' => __( 'Test', 'custom_blocks_test' ), 'id' => 'test', 'icon' => 'awards', 'category' => 'media', 'render_template' => plugin_dir_path( __FILE__ ) . 'template.php', 'type' => 'block', 'context' => 'side', 'fields' => [ [ 'name' => __( 'Headline', 'custom-blocks' ), 'id' => $prefix . 'headline', 'type' => 'text', ], [ 'name' => __( 'Radio', 'custom-blocks' ), 'id' => 'my_switch_id', 'type' => 'radio', 'inline' => false, 'options' => [ 'on' => 'Label1', 'off' => 'Label2', ], ], [ 'name' => __( 'Switch enabled', 'custom-blocks' ), 'id' => $prefix . 'my_switch_id', 'type' => 'switch', 'style' => 'rounded', 'on_label' => 'yes', 'off_label' => 'no', ], ], ]; return $meta_boxes; } Template: $headline = mb_get_block_field( 'headline' ); echo '<h3>'.$headline.'</h3>'; $value = rwmb_meta( 'my_radio_id' ); echo '<p>Selected: '.$value.'</p>'; $value = rwmb_meta( 'my_switch_id' ); if ( $value ) { echo '<p>Checked</p>'; } else { echo '<p>Unchecked</p>'; }
June 28, 2022 at 4:35 PM #36695Long Nguyen
ModeratorHi,
To get the field value in a block, please use the helper function
mb_get_block_field()
like the headline field. Read more on the documentation https://docs.metabox.io/extensions/mb-blocks/#render_callback$value = mb_get_block_field( 'my_radio_id' );
Note: You are using two fields with the same ID and it might not work correctly. Please change the radio field ID to another one
my_radio_id
.June 28, 2022 at 9:29 PM #36700metafan
ParticipantThank you!
-
AuthorPosts
Viewing 3 posts - 1 through 3 (of 3 total)
- You must be logged in to reply to this topic.