Alert: This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.
_inject_theme_attribute_in_block_template_content() WordPress Function
The inject_theme_attribute_in_block_template_content() function allows you to inject a theme attribute into a block template content. This can be useful if you want to include a specific CSS class or ID in your block template.
_inject_theme_attribute_in_block_template_content( string $template_content ) #
Parses wp_template content and injects the active theme’s stylesheet as a theme attribute into each wp_template_part
Parameters
- $template_content
(string)(Required)serialized wp_template content.
Return
(string) Updated 'wp_template' content.
Source
File: wp-includes/block-template-utils.php
function _inject_theme_attribute_in_block_template_content( $template_content ) {
$has_updated_content = false;
$new_content = '';
$template_blocks = parse_blocks( $template_content );
$blocks = _flatten_blocks( $template_blocks );
foreach ( $blocks as &$block ) {
if (
'core/template-part' === $block['blockName'] &&
! isset( $block['attrs']['theme'] )
) {
$block['attrs']['theme'] = wp_get_theme()->get_stylesheet();
$has_updated_content = true;
}
}
if ( $has_updated_content ) {
foreach ( $template_blocks as &$block ) {
$new_content .= serialize_block( $block );
}
return $new_content;
}
return $template_content;
}
Expand full source codeCollapse full source codeView on TracView on GitHub
Changelog
| Version | Description |
|---|---|
| 5.9.0 | Introduced. |