get_block_theme_folders() WordPress Function

The get_block_theme_folders() function allows you to programmatically get a list of all the theme folders that contain block templates. This is useful if you need to loop through all the themes and check if they have a certain template file.

get_block_theme_folders( string $theme_stylesheet = null ) #

For backward compatibility reasons, block themes might be using block-templates or block-template-parts, this function ensures we fallback to these folders properly.


Parameters

$theme_stylesheet

(string)(Optional)The stylesheet. Default is to leverage the main theme root.

Default value: null


Top ↑

Return

(string[]) Folder names used by block themes.

  • 'wp_template'
    (string) Theme-relative directory name for block templates.
  • 'wp_template_part'
    (string) Theme-relative directory name for block template parts.


Top ↑

Source

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

function get_block_theme_folders( $theme_stylesheet = null ) {
	$theme_name = null === $theme_stylesheet ? get_stylesheet() : $theme_stylesheet;
	$root_dir   = get_theme_root( $theme_name );
	$theme_dir  = "$root_dir/$theme_name";

	if ( file_exists( $theme_dir . '/block-templates' ) || file_exists( $theme_dir . '/block-template-parts' ) ) {
		return array(
			'wp_template'      => 'block-templates',
			'wp_template_part' => 'block-template-parts',
		);
	}

	return array(
		'wp_template'      => 'templates',
		'wp_template_part' => 'parts',
	);
}


Top ↑

Changelog

Changelog
VersionDescription
5.9.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.