wppaste
WordPress

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:1266
Retrieves a single unified template object using its id.

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

Reaches the database via get_post().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
12–66

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 69.

Plugin surface
2 hooks

Third-party callbacks on 'pre_get_block_template', 'get_block_template' run inside this call, and their cost is not bounded by anything here.

Called by
8

8 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • querycontent queryget_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.

WhenInstructionsCalls it makes
$block_template !== null12apply_filters()
$block_template === null21apply_filters(), explode()
$block_template === null && !is_wp_error()55apply_filters(), explode(), _build_block_template_result_from_post(), is_wp_error()
$block_template === null56apply_filters(), explode(), get_block_file_template(), apply_filters()
$block_template === null && is_wp_error()66apply_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:

  1. apply_filters( pre_get_block_template )filterline 1279 (+13 into the body)

    Filters the block template object before the query takes place.

  2. apply_filters( get_block_template )filterline 1325 (+59 into the body)

    Filters the queried block template object after it's been fetched.

Uses · 5

Used by · 8

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 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.