Support

Account

Home Forums Gutenberg Register block preview image with acf_register_block_type? Reply To: Register block preview image with acf_register_block_type?

  • The easiest way I think to do this is to pass the example array to the acf_register_block_type function and include “an additional non-custom-field value” as mentioned in the “Adding block previews 🌄” section of the [function’s docs](https://www.advancedcustomfields.com/resources/acf_register_block_type/).

    In the rendering template, include a conditional to check for the is_example flag and render a screenshot instead of the HTML, if it’s set. The screenshot could be an image, GIF, or even video (probably). See my conclusion after the code examples.

    // init.php
    acf_register_block_type([
    ...
    	'example' => [
    		'attributes' => [
    			'mode' => 'preview',
    			'data' => ['is_example' => true],
    		]
    	]
    ...
    ]);
    // block.php
    if (get_field('is_example')) :
    	/* Render screenshot for example */
    else :
    	/* Render HTML for block */
    endif;

    That’s how I *think* it should work, but I have an issue with get_field not returning data from the example array. Am I missing something? The docs are pretty clear on how to include data for the example render, but it’s not working at all for me. I’m also not seeing any of the data that I’m passing in the example array show up in the $block['data'] array as mentioned in the docs 😕