_remove_theme_attribute_in_block_template_content( string $template_content ): string

In this article

This function has been deprecated since 6.4.0. Use traverse_and_serialize_blocks( parse_blocks( $template_content ), ‘_remove_theme_attribute_from_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 a block template and removes the theme attribute from each template part.

Parameters

$template_contentstringrequired
Serialized block template content.

Return

string Updated block template content.

Source

function _remove_theme_attribute_in_block_template_content( $template_content ) {
	_deprecated_function(
		__FUNCTION__,
		'6.4.0',
		'traverse_and_serialize_blocks( parse_blocks( $template_content ), "_remove_theme_attribute_from_template_part_block" )'
	);

	$has_updated_content = false;
	$new_content         = '';
	$template_blocks     = parse_blocks( $template_content );

	$blocks = _flatten_blocks( $template_blocks );
	foreach ( $blocks as $key => $block ) {
		if ( 'core/template-part' === $block['blockName'] && isset( $block['attrs']['theme'] ) ) {
			unset( $blocks[ $key ]['attrs']['theme'] );
			$has_updated_content = true;
		}
	}

	if ( ! $has_updated_content ) {
		return $template_content;
	}

	foreach ( $template_blocks as $block ) {
		$new_content .= serialize_block( $block );
	}

	return $new_content;
}

Changelog

VersionDescription
6.4.0Deprecated. Use traverse_and_serialize_blocks( parse_blocks( $template_content ), '_remove_theme_attribute_from_template_part_block' ) instead.
5.9.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.