MB Block Not Saving Data

Support MB Blocks MB Block Not Saving DataResolved

Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #23669
    Jacob HillJacob Hill
    Participant

    Hello,

    I have one issue and one question:

    1. For some reason, my MB block data is not being saved! I populate the fields, click publish/update, refresh the page, and then my data is gone! Here is what the data looks like: <!-- wp:meta-box/request {"id":"mb-block-3f18ba60-8fff-4406-b411-450e2ffa92dd","data":[]} /-->
    2. I'd like to save the data in post_meta. I was researching how to do this, and it appears I have to use register_post_meta (not sure if MB already does this) and then add attributes possibly like so when I'm registering the block using rwmb_meta_boxes (no idea if I have the syntax right):
    'attributes' => array(
        $prefix . 'category' => array(
            'type' => 'string',
            'source' => 'meta',
            'meta' => $prefix . 'category'
        ),
        $prefix . 'description' => array(
            'type' => 'string',
            'source' => 'meta',
            'meta' => $prefix . 'description'
        ),
    ),

    This link and this link seem to be good references.

    Let me know and thank you!

    Jacob Hill

    #23673
    Long NguyenLong Nguyen
    Moderator

    Hi Jacob,

    1. Please share the code that creates the block, I will check it on my end.

    2. The extension MB Blocks has not supported meta for block yet. Instead, it saves the JSON string in the block content as introduce in the documentation https://docs.metabox.io/extensions/mb-blocks/#block-data.

    #23674
    Jacob HillJacob Hill
    Participant

    Hello! Thank you for the quick response! Regarding question 2, I'd love to get help with custom code to get this done since MB doesn't support it out of the box. But the 1st priority is solving question 1, as the data isn't saving. šŸ™‚

    Sure! Here is the CPT code:

    // Creates the CPT.
    add_action( 'init', 'tekfused_register_cpt_requests' );
    function tekfused_register_cpt_requests()
    {
        $args = array (
            'label' => esc_html__( 'Requests', 'tekfused-cpt-requests' ),
            'labels' => array(
                'menu_name' => esc_html__( 'Requests', 'tekfused-cpt-requests' ),
                'name_admin_bar' => esc_html__( 'Request', 'tekfused-cpt-requests' ),
                'add_new' => esc_html__( 'Add new', 'tekfused-cpt-requests' ),
                'add_new_item' => esc_html__( 'Add new Request', 'tekfused-cpt-requests' ),
                'new_item' => esc_html__( 'New Request', 'tekfused-cpt-requests' ),
                'edit_item' => esc_html__( 'Edit Request', 'tekfused-cpt-requests' ),
                'view_item' => esc_html__( 'View Request', 'tekfused-cpt-requests' ),
                'update_item' => esc_html__( 'Update Request', 'tekfused-cpt-requests' ),
                'all_items' => esc_html__( 'All Requests', 'tekfused-cpt-requests' ),
                'search_items' => esc_html__( 'Search Requests', 'tekfused-cpt-requests' ),
                'parent_item_colon' => esc_html__( 'Parent Request', 'tekfused-cpt-requests' ),
                'not_found' => esc_html__( 'No Requests found', 'tekfused-cpt-requests' ),
                'not_found_in_trash' => esc_html__( 'No Requests found in Trash', 'tekfused-cpt-requests' ),
                'name' => esc_html__( 'Requests', 'tekfused-cpt-requests' ),
                'singular_name' => esc_html__( 'Request', 'tekfused-cpt-requests' ),
            ),
            'public' => true,
            'exclude_from_search' => true,
            'publicly_queryable' => false,
            'show_ui' => true,
            'show_in_nav_menus' => true,
            'show_in_admin_bar' => true,
            'show_in_rest' => true,
            'menu_icon' => 'dashicons-list-view',
            'hierarchical' => false,
            'has_archive' => false,
            'template' => array(
                 array( 'meta-box/request' ),
            ),
            'template_lock' => 'all',
            'query_var' => false,
            'can_export' => true,
            'supports' => array(
                'title',
                'editor',
            ),
            'rewrite' => array(
                'slug' => 'requests',
            ),
            'capability_type' => array( 'request', 'requests' ),
            'map_meta_cap' => true,
        );
    
        register_post_type( 'tf-requests', $args );
    }

    And here is the MB block code:

    // Creates the fields.
    add_filter( 'rwmb_meta_boxes', 'tekfused_register_meta_box_requests_info' );
    function tekfused_register_meta_box_requests_info( $meta_boxes )
    {
        $prefix = '_tf_requests_';
    
        $meta_boxes[] = array (
            'title' => esc_html__( 'Request Info', 'tf-meta-box-request-info' ),
            'id' => 'request',
            'post_types' => array(
                0 => 'tf-requests',
            ),
            'context' => 'normal',
            'priority' => 'high',
            'revision' => true,
            'type' => 'block',
            'mode' => 'edit',
            'category' => 'common',
            'icon' => 'awards',
            'supports' => [
                 'multiple' => false,
            ],
            'keywords' => ['image', 'photo', 'pics'],
            'fields' => array(
                array (
                    'id' => $prefix . 'category',
                    'name' => esc_html__( 'Category', 'tf-meta-box-request-info' ),
                    'type' => 'select',
                    'required' => 1,
                    // 'placeholder' => esc_html__( 'Select a Category', 'tf-meta-box-request-info' ),
                    'tooltip' => esc_html__( 'The category of request.', 'tf-meta-box-request-info' ),
                    'options' => array(
                        // Blank option.
                        '' => esc_html__( '', 'tf-meta-box-request-info' ),
                        'Other' => esc_html__( 'Other', 'tf-meta-box-request-info' ),
                    ),
                ),
                array (
                    'id' => $prefix . 'description',
                    'type' => 'textarea',
                    'name' => esc_html__( 'Description', 'tf-meta-box-request-info' ),
                    'tooltip' => esc_html__( 'A description of the request.', 'tf-meta-box-request-info' ),
                ),
            ),
            'text_domain' => 'tf-meta-box-request-info',
            // Attempt to get this to save to Post Meta --- this is related to question 2, first issue to solve is why the data is not saving.
            /*
            'attributes' => array(
                $prefix . 'category' => array(
                    'type' => 'string',
                    'source' => 'meta',
                    'meta' => $prefix . 'category'
                ),
                $prefix . 'description' => array(
                    'type' => 'string',
                    'source' => 'meta',
                    'meta' => $prefix . 'description'
                ),
            ),
            */
        );
    
        return $meta_boxes;
    }

    Please let me know if you if you have any other questions - and thanks again for the fast response!

    Jacob Hill

    #23675
    Jacob HillJacob Hill
    Participant

    The reason I want to save the data into post meta is so I can access the field data using Beaver Builder, but still have the modern editing experience that Gutenberg provides on the back end.

    #23680
    Long NguyenLong Nguyen
    Moderator

    Hi,

    I’m going to create a feature request for the developer team to support saving custom fields as the post meta in the block. Thank you.

    #23683
    Jacob HillJacob Hill
    Participant

    Thank you!! That will be awesome! Any idea on the ETA for the feature?

    In the interim, do you know why my block isn't saving data (1), and for (2), if there is a custom code workaround to save the block to post meta?

    #23693
    Long NguyenLong Nguyen
    Moderator

    Hi,

    Please try to remove the $prefix variable of the field ID then check to save data again.

    array (
        'id' => 'description',
        'type' => 'textarea',
        'name' => esc_html__( 'Description', 'tf-meta-box-request-info' ),
        'tooltip' => esc_html__( 'A description of the request.', 'tf-meta-box-request-info' ),
    ),
    #23714
    Jacob HillJacob Hill
    Participant

    Hello! Removing the $prefix variable did allow the content to be saved! Any tips on the code needed to save to post meta? Thanks in advance!

    #23742
    Jacob HillJacob Hill
    Participant

    Hello! Just checking in for an update. Happy new year!

    Hello! Removing the $prefix variable did allow the content to be saved! Any tips on the code needed to save to post meta? Thanks in advance!

    #23743
    Long NguyenLong Nguyen
    Moderator

    Hi Jacob,

    The developer team is working to create a new update to support saving block meta. You can create your own block with JavaScript code by following the documentation https://developer.wordpress.org/block-editor/tutorials/block-tutorial/writing-your-first-block-type/.

    #23749
    Jacob HillJacob Hill
    Participant

    Thanks for the the update! Is there a rough estimate on when the feature will be added to Meta Box?

    #24193
    Jacob HillJacob Hill
    Participant

    Good Morning! Do you know if this enhancement (ability to save in post_meta from a block) was included in MB Builder 4? https://metabox.io/introducing-meta-box-builder-4/

    #24198
    Long NguyenLong Nguyen
    Moderator

    Hi Jacob,

    Thank you for reaching out.

    Not yet, this feature is the core of MB Blocks and it has been added to the to-do list of the developer team. It will be included in the future update.

    #24896
    Jacob HillJacob Hill
    Participant

    I see that the devs have added the ability to save Gutenberg blocks as post_meta! Thank you very much, devs!

    https://docs.metabox.io/extensions/mb-blocks/#storage_type

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