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?

  • Also managed to solve this reading trough this topic, thank you all! My solution may work for others so also posting it here:

    In my “block.json”:

    {
        "name": "acf/my-acf-block",
        "title": "My ACF Block",
        "description": "Description of this block.",
        "style": "file:./my-acf-block.css",
        "category": "my-custom-category",
        "icon": "random-icon-or-svg",
        "textdomain": "my-textdomain",
        "acf": {
            "mode": "edit",
            "renderTemplate": "my-acf-block.php"
        },
        "supports": {
            "align": [
                "full"
            ]
        },
        "example": {
            "attributes": {
                "mode": "preview",
                "data": {
                    "preview_image_my_acf_block": "assets/img/my-placeholder.jpg"
                }
            }
        }
    }

    And in “my-acf-block.php” I start with:

    <?php
    
    if( isset( $block['data']['preview_image_my_acf_block'] )  ) {
    	
    	echo '<img src="' . CONSTANT_THEME_URL . $block['data']['preview_image_my_acf_block'] . ' " style="width: 100%; height: auto;">';
    	
    	return;
    
    }
    
    // the rest of my template render..
    ?>
    

    The “CONSTANT_THEME_URL” is a constant that I normally define within my theme and returns the URL of the current theme, but including trailingslashit().