• Resolved Nasif

    (@nasifrr)


    Hello!

    I’m not really sure how much support you’re giving for CMB2 in the block editor, but I just wanted to let you know that when I added a ‘File’ field with this code:

    $post_mb->add_field( array(
        'name'    => 'Banner Image',
        'desc'    => 'Upload an image to be used as a banner,
        'id'      => 'post_banner',
        'type'    => 'file',
        // Optional:
        'options' => array(
            'url' => false, // Hide the text input for the url
        ),
        'text'    => array(
            'add_upload_file_text' => 'Add File' // Change upload button text. Default: "Add or Upload File"
        ),
        // query_args are passed to wp.media's library query.
        'query_args' => array(
            'type' => array(
                'image/jpeg',
                'image/png',
            ),
        ),
        'preview_size' => 'large', // Image size to use when previewing in the admin
      ) );

    The field rendered in its metabox but when a file was selected it didn’t seem to save it. It didn’t show a preview either and when I checked the database it did not add its corresponding post meta.

    Hope it helps, cmb2 has been a great lifesaver for me!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    I’m suspecting something else may be going on here for your specific case, since I was able to use the code above and successfully save as expected. Checked the meta table as well and the data was there. Refreshed the editor screen and everything came up like expected too.

    Not saying that this isn’t the same for you, but just there’s something else in regards to your site that may be interfering.

    Thread Starter Nasif

    (@nasifrr)

    This same code works for me when on the classic editor, but not on the block editor. Is there anything specific coming to your mind that could be interfering in my site and that I should check?

    Thanks!

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Not really. I’d end up opening up my browser’s dev tools and the console or network tab to try and observe anything odd coming through as I click save.

    Otherwise, I’d also end up doing “good ole” disable everything until it stops happening and then re-enable one by one until it happens again. Kind of a sledgehammer approach, but it technically does its job especially if able to be done on a development copy of a site.

    Thread Starter Nasif

    (@nasifrr)

    Thanks a lot Michael!

    I’m unsure of what was happening. My code was the same I posted in here except I was using my site’s prefix in the field ‘id’. I tried changing small parts and for some reason it all worked out when I changed the id to another string. Returning to the old string breaks it so I guess maybe I’m already using that meta key? Anyways, thanks for your help.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    meta keys are meant to be re-usable even in the same post, but that said, i’m never going to complain about things suddenly working. ??

    Let us know if you need anything else.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘File field fails to save in Block Editor’ is closed to new replies.