MB Block with switch or radio buttons

Support MB Blocks MB Block with switch or radio buttonsResolved

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #36682
    metafanmetafan
    Participant

    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>';
    }
    #36695
    Long NguyenLong Nguyen
    Moderator

    Hi,

    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.

    #36700
    metafanmetafan
    Participant

    Thank you!

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.