wppaste
WordPress

get_the_block_template_html(): string

Since
5.8.0
Source
wp-includes/block-template.php:249
Returns the markup for the current template.

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.

Return value

string
Block template markup.

Performance profile

How much work a call to get_the_block_template_html() 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

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Scales with input

The body loops, so the work grows with how much data it finds.

Instructions
9–60

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksapply_filters()one call below get_the_block_template_html()
  • optionoption read or writeget_option()one call below get_the_block_template_html()

Further down the call graph this can also reach cache, serialize, query 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 · 7 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost get_the_block_template_html() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!is_user_logged_in()9is_user_logged_in()
is_user_logged_in()14is_user_logged_in(), esc_html__()
always45->run_shortcode(), ->autoembed(), shortcode_unautop(), do_shortcode(), do_blocks(), wptexturize(), convert_smilies(), wp_filter_content_tags()
!$_wp_current_template_id50->run_shortcode(), ->autoembed(), shortcode_unautop(), do_shortcode(), get_stylesheet(), do_blocks(), wptexturize(), convert_smilies(), wp_filter_content_tags()
$_wp_current_template_id53–56->run_shortcode(), ->autoembed(), shortcode_unautop(), do_shortcode(), get_stylesheet(), is_singular(), do_blocks(), wptexturize(), convert_smilies(), wp_filter_content_tags()
$_wp_current_template_id && is_singular() && !have_posts()59->run_shortcode(), ->autoembed(), shortcode_unautop(), do_shortcode(), get_stylesheet(), is_singular(), have_posts(), do_blocks(), wptexturize(), convert_smilies(), wp_filter_content_tags()
$_wp_current_template_id && is_singular()60->run_shortcode(), ->autoembed(), shortcode_unautop(), do_shortcode(), get_stylesheet(), is_singular(), have_posts(), have_posts(), wptexturize(), convert_smilies(), wp_filter_content_tags()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev809–608
8.5809–608
8.4809–6086 fewer instructions than PHP 8.3
8.3869–668
8.2869–6682 more instructions than PHP 8.1
8.1849–748
7.4849–748

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.

Uses · 13

  • is_user_logged_in()Determines whether the current visitor is a logged in user.
  • esc_html__()Retrieves the translation of $text and escapes it for safe use in HTML output.
  • shortcode_unautop()Don't auto-p wrap shortcodes that stand alone.
  • do_shortcode()Searches content for shortcodes and filter shortcodes through their hooks.
  • str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
  • get_stylesheet()Retrieves name of the current stylesheet.
  • is_singular()Determines whether the query is for an existing single post of any post type (post, attachment, page, custom post types).
  • have_posts()Determines whether current WordPress query has posts to loop over.
  • the_post()Iterate the post index in the loop.
  • do_blocks()Parses dynamic blocks out of `post_content` and re-renders them.
  • wptexturize()Replaces common plain text characters with formatted entities.
  • convert_smilies()Converts text equivalent of smilies to images.
Show all 13

Source code

function get_the_block_template_html() {	global $_wp_current_template_id, $_wp_current_template_content, $wp_embed, $wp_query; 	if ( ! $_wp_current_template_content ) {		if ( is_user_logged_in() ) {			return '<h1>' . esc_html__( 'No matching template found' ) . '</h1>';		}		return;	} 	$content = $wp_embed->run_shortcode( $_wp_current_template_content );	$content = $wp_embed->autoembed( $content );	$content = shortcode_unautop( $content );	$content = do_shortcode( $content ); 	/*	 * Most block themes omit the `core/query` and `core/post-template` blocks in their singular content templates.	 * While this technically still works since singular content templates are always for only one post, it results in	 * the main query loop never being entered which causes bugs in core and the plugin ecosystem.	 *	 * The workaround below ensures that the loop is started even for those singular templates. The while loop will by	 * definition only go through a single iteration, i.e. `do_blocks()` is only called once. Additional safeguard	 * checks are included to ensure the main query loop has not been tampered with and really only encompasses a	 * single post.	 *	 * Even if the block template contained a `core/query` and `core/post-template` block referencing the main query	 * loop, it would not cause errors since it would use a cloned instance and go through the same loop of a single	 * post, within the actual main query loop.	 *	 * This special logic should be skipped if the current template does not come from the current theme, in which case	 * it has been injected by a plugin by hijacking the block template loader mechanism. In that case, entirely custom	 * logic may be applied which is unpredictable and therefore safer to omit this special handling on.	 */	if (		$_wp_current_template_id &&		str_starts_with( $_wp_current_template_id, get_stylesheet() . '//' ) &&		is_singular() &&		1 === $wp_query->post_count &&		have_posts()	) {		while ( have_posts() ) {			the_post();			$content = do_blocks( $content );		}	} else {		$content = do_blocks( $content );	} 	$content = wptexturize( $content );	$content = convert_smilies( $content );	$content = wp_filter_content_tags( $content, 'template' );	$content = str_replace( ']]>', ']]&gt;', $content ); 	// Wrap block template in .wp-site-blocks to allow for specific descendant styles	// (e.g. `.wp-site-blocks > *`).	return '<div class="wp-site-blocks">' . $content . '</div>';}

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.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.