lzb/template_exists
Filters the path Lazy Blocks tests with file_exists() when deciding whether a block has a theme template at all.
Source marks this one DEPRECATED. It is not an alias of lzb/block_render/template_exists: it takes three arguments where that one takes six, and it fires after it. Write new code against the longer name and reach for this one only to read what an existing handler does.
Attributes
| Name | Type | Description |
|---|---|---|
$template | String | absolute path resolved so far, empty when locate_template() found nothing |
$template_name | String | theme relative path being looked up, /blocks/lazyblock-my-block/block.php |
$args | Array | render arguments, see below |
$args has exactly four keys:
| Name | Type | Description |
|---|---|---|
attributes | Array | block attributes |
block | Array | block data |
render_location | String | editor or frontend |
context | WP_Block | null | block context provided by parent blocks |
Usage
function my_lzb_template_exists( $template, $template_name, $args ) {
// Let the block builder preview fall back to the frontend template
// instead of showing "template not found" while editor.php is missing.
if ( 'editor' !== $args['render_location'] ) {
return $template;
}
if ( $template && file_exists( $template ) ) {
return $template;
}
return locate_template( array( str_replace( '/editor.php', '/block.php', $template_name ) ) );
}
add_filter( 'lzb/template_exists', 'my_lzb_template_exists', 10, 3 );The return value is passed straight to file_exists(), so an empty string or a path that is not on disk sends the block to the template-not-found.php fallback. A handler here that returns a path must be matched by one on lzb/include_template returning the same path, otherwise Lazy Blocks confirms the template exists and then includes a different file.
Both filters run last in their function, after lzb/block_render/template_exists and after both per-slug variants, so they overrule anything those returned. The output method they belong to is Theme Template.