lzb/block_render/template_exists
Tells Lazy Blocks that a block template lives somewhere the theme lookup will never reach, such as inside a plugin that ships blocks of its own.
A block whose Output Method is Theme Template is rendered from /blocks/BLOCK-SLUG/block.php in the active theme, or /blocks/BLOCK-SLUG/editor.php in the editor. template_exists() resolves that name with locate_template(), fires this filter, then returns file_exists() on the result. So the filter decides whether the template is considered to be there at all, and its partner lzb/block_render/include_template decides which file is actually loaded. Filter one without the other and Lazy Blocks reports a template it then fails to include.
Attributes
| Name | Type | Description |
|---|---|---|
$template | String | path found by locate_template(), empty when the theme has no such file |
$template_name | String | name being looked up, for example /blocks/lazyblock-recipe/block.php |
$attributes | Array | control values, as the template will see them |
$block | Array | block data, the same array lzb/block_data returns |
$render_location | String | editor or frontend |
$context | Array | null | block context from parent blocks |
Additional Filters
| Name | Arguments | Description |
|---|---|---|
lazyblock/BLOCK_SLUG/frontend_template_exists | $template, $template_name, $attributes, $block, $context | specific block in the frontend only |
lazyblock/BLOCK_SLUG/editor_template_exists | $template, $template_name, $attributes, $block, $context | specific block in the editor only |
lazyblock/BLOCK_SLUG/template_exists | $template, $template_name, $attributes, $block, $render_location, $context | specific block only |
A shorter filter named lzb/template_exists is still in the code for compatibility. It takes ( $template, $template_name, $args ), where $args is the whole array holding attributes, block, render_location and context rather than those four unpacked. It fires after every filter in the table above, so whatever it returns wins. New code should use the block_render name. See lzb/template_exists.
Usage
function my_lzb_block_render_template_exists( $template, $template_name, $attributes, $block, $render_location, $context ) {
// Serve block templates from the plugin when the theme has none, so a
// theme switch does not take the blocks down with it.
if ( $template ) {
return $template;
}
$fallback = plugin_dir_path( __FILE__ ) . ltrim( $template_name, '/' );
return file_exists( $fallback ) ? $fallback : $template;
}
add_filter( 'lzb/block_render/template_exists', 'my_lzb_block_render_template_exists', 10, 6 );Return a path, not a boolean. The name reads like a yes or no question, but Lazy Blocks calls file_exists() on your return value, so true becomes the string 1 and the check fails. Returning an empty string sends the block to templates/template-not-found.php inside the plugin, which prints a "Template file not found" notice where the block should have been.
Register the same fallback on lzb/block_render/include_template, or the file is found and never loaded. Theme templates are covered in Theme Template.