On this page
_get_block_templates_files( string $template_type ): array
This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.
Retrieves the template files from the theme.
Parameters
$template_typestring Required-
'wp_template'or'wp_template_part'.
Return
array Template.
Source
File: wp-includes/block-template-utils.php. View all references
function _get_block_templates_files( $template_type ) {
if ( 'wp_template' !== $template_type && 'wp_template_part' !== $template_type ) {
return null;
}
$themes = array(
get_stylesheet() => get_stylesheet_directory(),
get_template() => get_template_directory(),
);
$template_files = array();
foreach ( $themes as $theme_slug => $theme_dir ) {
$template_base_paths = get_block_theme_folders( $theme_slug );
$theme_template_files = _get_block_templates_paths( $theme_dir . '/' . $template_base_paths[ $template_type ] );
foreach ( $theme_template_files as $template_file ) {
$template_base_path = $template_base_paths[ $template_type ];
$template_slug = substr(
$template_file,
// Starting position of slug.
strpos( $template_file, $template_base_path . DIRECTORY_SEPARATOR ) + 1 + strlen( $template_base_path ),
// Subtract ending '.html'.
-5
);
$new_template_item = array(
'slug' => $template_slug,
'path' => $template_file,
'theme' => $theme_slug,
'type' => $template_type,
);
if ( 'wp_template_part' === $template_type ) {
$template_files[] = _add_block_template_part_area_info( $new_template_item );
}
if ( 'wp_template' === $template_type ) {
$template_files[] = _add_block_template_info( $new_template_item );
}
}
}
return $template_files;
}
Related
Uses
| Uses | Description |
|---|---|
| _get_block_templates_paths() wp-includes/block-template-utils.php | Finds all nested template part file paths in a theme’s directory. |
| _add_block_template_part_area_info() wp-includes/block-template-utils.php | Attempts to add the template part’s area information to the input template. |
| _add_block_template_info() wp-includes/block-template-utils.php | Attempts to add custom template information to the template item. |
| get_block_theme_folders() wp-includes/block-template-utils.php | For backward compatibility reasons, block themes might be using block-templates or block-template-parts, this function ensures we fallback to these folders properly. |
| get_stylesheet_directory() wp-includes/theme.php | Retrieves stylesheet directory path for the active theme. |
| get_template() wp-includes/theme.php | Retrieves name of the active theme. |
| get_template_directory() wp-includes/theme.php | Retrieves template directory path for the active theme. |
| get_stylesheet() wp-includes/theme.php | Retrieves name of the current stylesheet. |
Used By
| Used By | Description |
|---|---|
| get_block_templates() wp-includes/block-template-utils.php | Retrieves a list of unified template objects based on a query. |
Changelog
| Version | Description |
|---|---|
| 5.9.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/_get_block_templates_files