Skip to content

Assets are not enqueued for block template parts #2696

@dotsam

Description

@dotsam

Description

When an Otter Blocks block is placed in a block-based template part that is part of a classic theme, no assets are enqueued for it.

More details on block template parts in classic themes:

Searching the wordpress.org plugin directory, usage of block_template_part() seems to be extremely low, but a search on GitHub shows it to be a little more common in "private" themes.

Step-by-step reproduction instructions

  1. Insert any Otter Blocks block into a template part for a non-FSE theme
  2. Observe that functions correctly in the editor
  3. On the frontend, the block will not render correctly as assets are not enqueued

Screenshots, screen recording, code snippet or Help Scout ticket

ThemeIsle\GutenbergBlocks\Registration::enqueue_block_assets and ThemeIsle\GutenbergBlocks\CSS\Block_Frontend::enqueue_fse_css both have a hard check for is_block_theme()

if ( function_exists( 'get_block_templates' ) && function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() && current_theme_supports( 'block-templates' ) ) {
$this->enqueue_dependencies( 'block-templates' );
}

if ( ! ( function_exists( 'get_block_templates' ) && function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() && current_theme_supports( 'block-templates' ) ) ) {
return;
}

And then each only parse the "private" "global" $_wp_current_template_content for template parts (this is also similar functionality that coule be abstracted)

global $_wp_current_template_content;
$slugs = array();
$template_blocks = parse_blocks( $_wp_current_template_content );
foreach ( $template_blocks as $template_block ) {
if ( 'core/template-part' === $template_block['blockName'] ) {
$slugs[] = $template_block['attrs']['slug'];
}
}
$templates_parts = get_block_templates( array( 'slugs__in' => $slugs ), 'wp_template_part' );
foreach ( $templates_parts as $templates_part ) {
if ( ! empty( $templates_part->content ) && ! empty( $templates_part->slug ) && in_array( $templates_part->slug, $slugs ) ) {
$content .= $templates_part->content;
}
}
$content .= $_wp_current_template_content;
$post = $content;

global $_wp_current_template_content;
$content = '';
$slugs = array();
$template_blocks = parse_blocks( $_wp_current_template_content );
foreach ( $template_blocks as $template_block ) {
if ( 'core/template-part' === $template_block['blockName'] ) {
$slugs[] = $template_block['attrs']['slug'];
}
}
$templates_parts = get_block_templates( array( 'slugs__in' => $slugs ), 'wp_template_part' );
foreach ( $templates_parts as $templates_part ) {
if ( ! empty( $templates_part->content ) && ! empty( $templates_part->slug ) && in_array( $templates_part->slug, $slugs ) ) {
$content .= $templates_part->content;
}
}
$content .= $_wp_current_template_content;
$blocks = parse_blocks( $content );

This isn't a super easy problem to solve as WP core doesn't have any way to indicate if a template part called from block_template_part() has actually been displayed or not.

I'm currently using the following local patch to allow these methods to run, and then add a filter that I can hook in to with local logic for what template parts I know my theme is displaying:

diff -Naru otter-blocks/inc/class-registration.php otter-blocks-patched/inc/class-registration.php
--- otter-blocks/inc/class-registration.php	2025-10-09 15:41:36
+++ otter-blocks-patched/inc/class-registration.php	2025-10-10 09:45:24
@@ -386,7 +386,7 @@

 		}

-		if ( function_exists( 'get_block_templates' ) && function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() && current_theme_supports( 'block-templates' ) ) {
+		if ( function_exists( 'get_block_templates' ) && ( current_theme_supports( 'block-templates' ) || current_theme_supports( 'block-template-parts' ) ) ) {
 			$this->enqueue_dependencies( 'block-templates' );
 		}
 	}
@@ -414,6 +414,8 @@
 					$slugs[] = $template_block['attrs']['slug'];
 				}
 			}
+
+			$slugs = apply_filters( 'otter_blocks_fse_template_parts' , $slugs);

 			$templates_parts = get_block_templates( array( 'slugs__in' => $slugs ), 'wp_template_part' );

diff -Naru otter-blocks/inc/css/class-block-frontend.php otter-blocks-patched/inc/css/class-block-frontend.php
--- otter-blocks/inc/css/class-block-frontend.php	2025-10-09 16:45:09
+++ otter-blocks-patched/inc/css/class-block-frontend.php	2025-10-10 09:46:10
@@ -577,7 +577,7 @@
 	 * @access  public
 	 */
 	public function enqueue_fse_css() {
-		if ( ! ( function_exists( 'get_block_templates' ) && function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() && current_theme_supports( 'block-templates' ) ) ) {
+		if ( ! ( function_exists( 'get_block_templates' ) && ( current_theme_supports( 'block-templates' ) || current_theme_supports( 'block-template-parts' ) ) ) ) {
 			return;
 		}

@@ -592,6 +592,8 @@
 				$slugs[] = $template_block['attrs']['slug'];
 			}
 		}
+
+		$slugs = apply_filters( 'otter_blocks_fse_template_parts' , $slugs);

 		$templates_parts = get_block_templates( array( 'slugs__in' => $slugs ), 'wp_template_part' );

Environment info

No response

Is the issue you are reporting a regression

No

Metadata

Metadata

Labels

bugThis label could be used to identify issues that are caused by a defect in the product.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions