Title: _inject_theme_attribute_in_block_template_content
Published: February 3, 2022
Last modified: April 28, 2025

---

# _inject_theme_attribute_in_block_template_content( string $template_content ): string

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/_inject_theme_attribute_in_block_template_content/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/_inject_theme_attribute_in_block_template_content/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/_inject_theme_attribute_in_block_template_content/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/_inject_theme_attribute_in_block_template_content/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/_inject_theme_attribute_in_block_template_content/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/_inject_theme_attribute_in_block_template_content/?output_format=md#wp--skip-link--target)

This function has been deprecated since 6.4.0. Use traverse_and_serialize_blocks(
parse_blocks( $template_content ), ‘_inject_theme_attribute_in_template_part_block’)
instead.

This function’s access is marked private. This means it is not intended for use 
by plugin or theme developers, only by core. It is listed here for completeness.

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

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/_inject_theme_attribute_in_block_template_content/?output_format=md#parameters)󠁿

 `$template_content`stringrequired

serialized wp_template content.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/_inject_theme_attribute_in_block_template_content/?output_format=md#return)󠁿

 string Updated `'wp_template'` content.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/_inject_theme_attribute_in_block_template_content/?output_format=md#source)󠁿

    ```php
    function _inject_theme_attribute_in_block_template_content( $template_content ) {
    	_deprecated_function(
    		__FUNCTION__,
    		'6.4.0',
    		'traverse_and_serialize_blocks( parse_blocks( $template_content ), "_inject_theme_attribute_in_template_part_block" )'
    	);

    	$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 all references](https://developer.wordpress.org/reference/files/wp-includes/deprecated.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/deprecated.php#L6066)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/deprecated.php#L6066-L6097)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/_inject_theme_attribute_in_block_template_content/?output_format=md#related)󠁿

| Uses | Description | 
| [_flatten_blocks()](https://developer.wordpress.org/reference/functions/_flatten_blocks/)`wp-includes/block-template-utils.php` |

Returns an array containing the references of the passed blocks and their inner blocks.

  | 
| [serialize_block()](https://developer.wordpress.org/reference/functions/serialize_block/)`wp-includes/blocks.php` |

Returns the content of a block, including comment delimiters, serializing all attributes from the given parsed block.

  | 
| [parse_blocks()](https://developer.wordpress.org/reference/functions/parse_blocks/)`wp-includes/blocks.php` |

Parses blocks out of a content string.

  | 
| [get_stylesheet()](https://developer.wordpress.org/reference/functions/get_stylesheet/)`wp-includes/theme.php` |

Retrieves name of the current stylesheet.

  | 
| [_deprecated_function()](https://developer.wordpress.org/reference/functions/_deprecated_function/)`wp-includes/functions.php` |

Marks a function as deprecated and inform when it has been used.

  |

[Show 1 more](https://developer.wordpress.org/reference/functions/_inject_theme_attribute_in_block_template_content/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/_inject_theme_attribute_in_block_template_content/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/_inject_theme_attribute_in_block_template_content/?output_format=md#changelog)󠁿

| Version | Description | 
| [6.4.0](https://developer.wordpress.org/reference/since/6.4.0/) | Deprecated. Use traverse_and_serialize_blocks( parse_blocks( $template_content ), `'_inject_theme_attribute_in_template_part_block'` ) instead. | 
| [5.9.0](https://developer.wordpress.org/reference/since/5.9.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2F_inject_theme_attribute_in_block_template_content%2F)
before being able to contribute a note or feedback.