BlockTemplateUtils::get_theme_template_path()public staticWC 1.0

Gets the first matching template part within themes directories

Since Gutenberg 12.1.0, the conventions for block templates and parts directory has changed from block-templates and block-templates-parts to templates and parts respectively.

This function traverses all possible combinations of directory paths where a template or part could be located and returns the first one which is readable, prioritizing the new convention over the deprecated one, but maintaining that one for backwards compatibility.

Метод класса: BlockTemplateUtils{}

Хуков нет.


Строку|null. The matched path or null if no match was found.


$result = BlockTemplateUtils::get_theme_template_path( $template_slug, $template_type );
$template_slug(строка) (обязательный)
The slug of the template (i.e. without the file extension).
Either wp_template or wp_template_part.
По умолчанию: 'wp_template'

Код BlockTemplateUtils::get_theme_template_path() WC 7.5.1

public static function get_theme_template_path( $template_slug, $template_type = 'wp_template' ) {
	$template_filename      = $template_slug . '.html';
	$possible_templates_dir = 'wp_template' === $template_type ? array(
	) : array(

	// Combine the possible root directory names with either the template directory
	// or the stylesheet directory for child themes.
	$possible_paths = array_reduce(
		function( $carry, $item ) use ( $template_filename ) {
			$filepath = DIRECTORY_SEPARATOR . $item . DIRECTORY_SEPARATOR . $template_filename;

			$carry[] = get_stylesheet_directory() . $filepath;
			$carry[] = get_template_directory() . $filepath;

			return $carry;

	// Return the first matching.
	foreach ( $possible_paths as $path ) {
		if ( is_readable( $path ) ) {
			return $path;

	return null;