get_block_template( string $id, string $template_type = 'wp_template' ): WP_Block_Template|null
- Since
- 5.8.0
- Source
wp-includes/block-template-utils.php:1223
Compatibility
- WordPress
- since 5.8.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$idstring- Template unique identifier (example: 'theme_slug//template_slug').
$template_typestringoptional- Template type. Either 'wp_template' or 'wp_template_part'.
Default 'wp_template'.Default:'wp_template'
Return value
WP_Block_Template|null- Template.
Performance profile
How much work a call to get_block_template() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Constant
- Instructions
- 12–66
- Plugin surface
- 2 hooks
- Called by
- 8
Reaches the database via get_post().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 69.
Third-party callbacks on 'pre_get_block_template', 'get_block_template' run inside this call, and their cost is not bounded by anything here.
8 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - querycontent query
get_post()one call below get_block_template()
Further down the call graph this can also reach cache, option, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 5 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost get_block_template() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
$block_template !== null | 12 | apply_filters() |
$block_template === null | 21 | apply_filters(), explode() |
$block_template === null && !is_wp_error() | 55 | apply_filters(), explode(), _build_block_template_result_from_post(), is_wp_error() |
$block_template === null | 56 | apply_filters(), explode(), get_block_file_template(), apply_filters() |
$block_template === null && is_wp_error() | 66 | apply_filters(), explode(), _build_block_template_result_from_post(), is_wp_error(), get_block_file_template(), apply_filters() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 69 instructions, 12–66 executed per call, 4 branches. The work does not change between versions.
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 2
2 hooks fire while get_block_template() runs, in this order:
- apply_filters( pre_get_block_template )filterline 1236 (+13 into the body)
Filters the block template object before the query takes place.
- apply_filters( get_block_template )filterline 1282 (+59 into the body)
Filters the queried block template object after it's been fetched.
Uses · 5
- apply_filters()Calls the callback functions that have been added to a filter hook.
- _build_block_template_result_from_post()Builds a unified template object based a post Object.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- get_block_file_template()Retrieves a unified template object based on a theme file.
- WP_Query::__construct()Constructor.
Used by · 8
- WP_REST_Template_Revisions_Controller::get_parent()Gets the parent post, if the template ID is valid.
- WP_REST_Templates_Controller::create_item()Creates a single template.
- WP_REST_Templates_Controller::delete_item()Deletes a single template.
- WP_REST_Templates_Controller::get_item()Returns the given template
- WP_REST_Templates_Controller::prepare_item_for_database()Prepares a single template for create or update.
- WP_REST_Templates_Controller::prepare_links()Prepares links for the request.
- WP_REST_Templates_Controller::update_item()Updates a single template.
- block_template_part()Prints a block template part.
Source code
function get_block_template( $id, $template_type = 'wp_template' ) { /** * Filters the block template object before the query takes place. * * Return a non-null value to bypass the WordPress queries. * * @since 5.9.0 * * @param WP_Block_Template|null $block_template Return block template object to short-circuit the default query, * or null to allow WP to run its normal queries. * @param string $id Template unique identifier (example: 'theme_slug//template_slug'). * @param string $template_type Template type. Either 'wp_template' or 'wp_template_part'. */ $block_template = apply_filters( 'pre_get_block_template', null, $id, $template_type ); if ( ! is_null( $block_template ) ) { return $block_template; } $parts = explode( '//', $id, 2 ); if ( count( $parts ) < 2 ) { return null; } list( $theme, $slug ) = $parts; $wp_query_args = array( 'post_name__in' => array( $slug ), 'post_type' => $template_type, 'post_status' => array( 'auto-draft', 'draft', 'publish', 'trash' ), 'posts_per_page' => 1, 'no_found_rows' => true, 'tax_query' => array( array( 'taxonomy' => 'wp_theme', 'field' => 'name', 'terms' => $theme, ), ), ); $template_query = new WP_Query( $wp_query_args ); $posts = $template_query->posts; if ( count( $posts ) > 0 ) { $template = _build_block_template_result_from_post( $posts[0] ); if ( ! is_wp_error( $template ) ) { return $template; } } $block_template = get_block_file_template( $id, $template_type ); /** * Filters the queried block template object after it's been fetched. * * @since 5.9.0 * * @param WP_Block_Template|null $block_template The found block template, or null if there isn't one. * @param string $id Template unique identifier (example: 'theme_slug//template_slug'). * @param string $template_type Template type. Either 'wp_template' or 'wp_template_part'. */ return apply_filters( 'get_block_template', $block_template, $id, $template_type );}Changelog
Introduced in 5.8.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 tag, from
src/wp-includes/block-template-utils.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.