lzb/block_render/include_template
Chooses which file is loaded for a block whose Output Method is Theme Template, which is how a plugin ships block templates that no theme carries.
include_template() resolves the name with locate_template(), fires this filter, and includes the result when file_exists() passes. It runs after lzb/block_render/template_exists has already decided that a template is there, and the two resolve the path independently. Point one somewhere new without the other and Lazy Blocks finds a template it then declines to load, or the reverse.
The two are not symmetrical. template_exists receives $template_name, this one does not. Its second argument is the attributes array, and the file name is only recoverable from the block slug in $block['slug'].
Attributes
| Name | Type | Description |
|---|---|---|
$template | String | path found by locate_template(), empty when the theme has no such file |
$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 |
The included file gets $attributes, $block, $render_location and $context as local variables, because include_template() runs extract() on the same array before resolving the path.
Additional Filters
| Name | Arguments | Description |
|---|---|---|
lazyblock/BLOCK_SLUG/frontend_include_template | $template, $attributes, $block, $context | specific block in the frontend only |
lazyblock/BLOCK_SLUG/editor_include_template | $template, $attributes, $block, $context | specific block in the editor only |
lazyblock/BLOCK_SLUG/include_template | $template, $attributes, $block, $render_location, $context | specific block only |
A shorter filter named lzb/include_template 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/include_template.
Usage
function my_lzb_block_render_include_template( $template, $attributes, $block, $render_location, $context ) {
// Load block templates from the plugin when the theme has none.
if ( $template ) {
return $template;
}
$slug = str_replace( '/', '-', $block['slug'] );
$file = 'editor' === $render_location ? 'editor.php' : 'block.php';
$fallback = plugin_dir_path( __FILE__ ) . 'blocks/' . $slug . '/' . $file;
return file_exists( $fallback ) ? $fallback : $template;
}
add_filter( 'lzb/block_render/include_template', 'my_lzb_block_render_include_template', 10, 5 );This filter also fires for the plugin's own templates/template-not-found.php, which is what gets included when no template was found, so a handler that returns a path unconditionally replaces the not-found notice as well. A path that does not exist means nothing is included and the block renders as an empty string, which on the front end makes WordPress print no wrapper for it either.
Register the matching path on lzb/block_render/template_exists. Theme templates are covered in Theme Template.