Automattic\WooCommerce\Blocks\Utils
BlockTemplateUtils::get_theme_template_path()
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.
Method of the class: BlockTemplateUtils{}
No Hooks.
Return
String|null
. The matched path or null if no match was found.
Usage
$result = BlockTemplateUtils::get_theme_template_path( $template_slug, $template_type );
- $template_slug(string) (required)
- The slug of the template (i.e. without the file extension).
- $template_type(string)
- Either wp_template or wp_template_part.
Default: 'wp_template'
BlockTemplateUtils::get_theme_template_path() BlockTemplateUtils::get theme template path code WC 9.4.2
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( self::DIRECTORY_NAMES['TEMPLATES'], self::DIRECTORY_NAMES['DEPRECATED_TEMPLATES'], ) : array( self::DIRECTORY_NAMES['TEMPLATE_PARTS'], self::DIRECTORY_NAMES['DEPRECATED_TEMPLATE_PARTS'], ); // Combine the possible root directory names with either the template directory // or the stylesheet directory for child themes. $possible_paths = array_reduce( $possible_templates_dir, 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; }, array() ); // Return the first matching. foreach ( $possible_paths as $path ) { if ( is_readable( $path ) ) { return $path; } } return null; }