lzb_pro/block_style/template
Points a block at a stylesheet the theme does not carry, which is what a plugin shipping its own template blocks needs.
A block whose Output Method is Theme Template picks up two optional stylesheets next to its template, blocks/BLOCK-SLUG/block.css and blocks/BLOCK-SLUG/editor.css. find_style_template() resolves each name with locate_template(), fires this filter, and registers the file when file_exists() passes. Blocks using any other output method never reach it, because their CSS comes from the block builder's own style fields.
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, blocks/BLOCK-SLUG/block.css or blocks/BLOCK-SLUG/editor.css |
$block | Array | block data, the same array lzb/block_data returns |
$context | String | block for the front end stylesheet, editor for the editor one |
$context is not the render location. It names which of the two stylesheets is being resolved, so a handler that only wants to replace the front end file checks for block.
Additional Filters
| Name | Arguments | Description |
|---|---|---|
lazyblock/BLOCK_SLUG/block_style_template | $template, $template_name, $block | one block's front end stylesheet |
lazyblock/BLOCK_SLUG/editor_style_template | $template, $template_name, $block | one block's editor stylesheet |
lazyblock/BLOCK_SLUG/style_template | $template, $template_name, $block, $context | one block, both stylesheets |
Usage
function my_lzb_pro_block_style_template( $template, $template_name, $block, $context ) {
// Ship the front end CSS with the plugin, and let the theme override it
// by placing a file of the same name.
if ( $template || 'block' !== $context ) {
return $template;
}
$fallback = plugin_dir_path( __FILE__ ) . $template_name;
return file_exists( $fallback ) ? $fallback : $template;
}
add_filter( 'lzb_pro/block_style/template', 'my_lzb_pro_block_style_template', 10, 4 );Return an absolute filesystem path, not a URL. Lazy Blocks Pro reads the file's modification time to build a cache-busting version and then converts the path to a URL by matching it against the content, theme, child theme and plugin directories in that order. A file outside all of them falls back to matching against ABSPATH, and a path that matches nothing produces no stylesheet at all.
The script counterpart is lzb_pro/block_script/template. Where these files live is covered in Styles and Scripts.