The get_block_file_template() WordPress PHP function retrieves a unified template object based on a theme file. This function serves as a fallback for get_block_template() when no templates are found in the database.
Usage
To use the get_block_file_template() function, provide the unique template identifier and the optional template type as parameters:
get_block_file_template('theme_slug/template_slug', 'wp_template');
Parameters
- $id (string) – Required. The unique template identifier (e.g. ‘theme_slug/template_slug’).
- $template_type (string) – Optional. The template type: ‘wp_template’ or ‘wp_template_part’. Default is ‘wp_template’.
More information
See WordPress Developer Resources: get_block_file_template
Examples
Retrieve a ‘wp_template’ File
Retrieve a unified template object based on the theme file ‘mytheme/header’:
$template = get_block_file_template('mytheme/header');
Retrieve a ‘wp_template_part’ File
Retrieve a unified template object for a ‘wp_template_part’ based on the theme file ‘mytheme/partial’:
$template_part = get_block_file_template('mytheme/partial', 'wp_template_part');
Check if a Template File Exists
Check if a template file ‘mytheme/footer’ exists and then retrieve the template object:
if (get_block_file_template('mytheme/footer')) { $template = get_block_file_template('mytheme/footer'); }
Retrieve and Display Template Content
Retrieve the template object for ‘mytheme/sidebar’ and display its content:
$template = get_block_file_template('mytheme/sidebar'); echo $template->content;
Retrieve Template with a Dynamic Theme Slug
Retrieve a template object using the current theme slug and a dynamic template slug:
$theme_slug = get_template(); $template_slug = 'custom-template'; $template = get_block_file_template("$theme_slug/$template_slug");