_inject_theme_attribute_in_block_template_content() – Parses wp_template content and injects the active theme’s stylesheet as a theme attribute into each wp_template_part

You appear to be a bot. Output may be restricted

Description

Parses wp_template content and injects the active theme's stylesheet as a theme attribute into each wp_template_part

Usage

$string = _inject_theme_attribute_in_block_template_content( $template_content );

Parameters

$template_content
( string ) required – serialized wp_template content.

Returns

string Updated 'wp_template' content.

Source

File name: wordpress/wp-includes/block-template-utils.php


Lines:

1 to 28 of 28
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'] = 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;
}
 

 View on GitHub View on Trac