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?

  • I found a workaround since the above solution wasn’t working for me, and is kind of a pain if using get_fields(). Turns out you don’t need to add any custom data to the preview. Turns out when fetching the preview, WordPress includes the fact that it is a preview within the $_POST params.

    So you can just set up the example like this when registering the block

    
    'example'           => [
      'attributes'  => [
        'mode'  => 'preview',
        'data'  => [],
      ]
    ],
    

    Then when rendering the content check if it is a preview like this

    
    if( ! empty( $_POST['query']['preview'] ) :
      /* Render screenshot for example */
    else :
      /* Render HTML for block */
    endif;