Skip to content
PHP Filters

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

NameTypeDescription
$templateStringpath found by locate_template(), empty when the theme has no such file
$template_nameStringname being looked up, for example /blocks/lazyblock-recipe/block.php
$attributesArraycontrol values, as the template will see them
$blockArrayblock data, the same array lzb/block_data returns
$render_locationStringeditor or frontend
$contextArray | nullblock context from parent blocks

Additional Filters

NameArgumentsDescription
lazyblock/BLOCK_SLUG/frontend_template_exists$template, $template_name, $attributes, $block, $contextspecific block in the frontend only
lazyblock/BLOCK_SLUG/editor_template_exists$template, $template_name, $attributes, $block, $contextspecific block in the editor only
lazyblock/BLOCK_SLUG/template_exists$template, $template_name, $attributes, $block, $render_location, $contextspecific 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

PHP
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.

Was this article helpful?

Copyright © 2026 Lazy Blocks.