Hi,
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>';
}