Support

Account

Home Forums General Issues Creating ACF blocks with a plugin does not find the render_template Reply To: Creating ACF blocks with a plugin does not find the render_template

  • acf_register_block_type says “(String) The path to a template file used to render the block HTML. This can either be a relative path to a file within the active theme or a full path to any file.” – so you were probably referencing a file in theme, not in your plugin by accident 🙂

    However if you use the block.json for the registering config as was recommended later, there’s this: renderTemplate (string) The path to a template file used to render the block HTML. This can either be a relative path to a file based on where your block.json file is located, or an absolute path.

    So either way, an absolute path in PHP should always work, because you leave no room for magic, however I recommend using the block.json in a similar way as WP does to be consistent – however **keep in mind** renderTemplate does NOT contain the ‘file:’ prefix! (like WP render’s does) so instead of

    `
    “renderTemplate”: “file:./render.php” // as you would do in WP render: …
    `

    you have to do

    `
    “renderTemplate”: “render.php”
    `

    and this file would be placed in the SAME directory as the block.json, so if block.json is in .../wp-content/themes/yourtheme/blocks/your-block/block.json, then the rendering file will be in .../wp-content/themes/yourtheme/blocks/your-block/render.php

    In .../wp-content/themes/yourtheme/functions.php you would register such block just with this:

    `
    add_action(‘init’, function () {
    register_block_type( __DIR__ . ‘/blocks/your-block’ );
    // or if you want to be more specific, then
    // register_block_type( __DIR__ . ‘/blocks/your-block/block.json’ );
    });
    `

    PHP note: __DIR__ is a special constant referencing AN ABSOLUTE PATH to the folder of the current code’s file (of the __FILE__). So for /system/wp-content/themes/yourtheme/functions.php it’s /system/wp-content/themes/yourtheme – this has been available since 5.3…

    If you don’t know how to setup block.json – see Create Your First ACF Block – Registering Blocks with block.json