Title: get_the_block_template_html
Published: July 20, 2021
Last modified: August 20, 2026

---

# get_the_block_template_html(): string

## In this article

 * [Return](https://developer.wordpress.org/reference/functions/get_the_block_template_html/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/get_the_block_template_html/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/get_the_block_template_html/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/get_the_block_template_html/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/get_the_block_template_html/?output_format=md#wp--skip-link--target)

This function’s access is marked private. This means it is not intended for use 
by plugin or theme developers, only by core. It is listed here for completeness.

Returns the markup for the current template.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/get_the_block_template_html/?output_format=md#return)󠁿

 string Block template markup.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/get_the_block_template_html/?output_format=md#source)󠁿

    ```php
    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 > *`).
    	$template_html = '<div class="wp-site-blocks">' . $content . '</div>';

    	// Back-compat for plugins that disable functionality by unhooking one of these actions.
    	if (
    		! has_action( 'wp_footer', 'the_block_template_skip_link' ) ||
    		! has_action( 'wp_enqueue_scripts', 'wp_enqueue_block_template_skip_link' )
    	) {
    		return $template_html;
    	}

    	return _block_template_add_skip_link( $template_html );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/block-template.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/block-template.php#L249)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/block-template.php#L249-L315)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/get_the_block_template_html/?output_format=md#related)󠁿

| Uses | Description | 
| [_block_template_add_skip_link()](https://developer.wordpress.org/reference/functions/_block_template_add_skip_link/)`wp-includes/block-template.php` |

Inserts the block template skip-link into the template HTML.

  | 
| [wp_filter_content_tags()](https://developer.wordpress.org/reference/functions/wp_filter_content_tags/)`wp-includes/media.php` |

Filters specific tags in post content and modifies their markup.

  | 
| [do_blocks()](https://developer.wordpress.org/reference/functions/do_blocks/)`wp-includes/blocks.php` |

Parses dynamic blocks out of `post_content` and re-renders them.

  | 
| [get_stylesheet()](https://developer.wordpress.org/reference/functions/get_stylesheet/)`wp-includes/theme.php` |

Retrieves name of the current stylesheet.

  | 
| [esc_html__()](https://developer.wordpress.org/reference/functions/esc_html__/)`wp-includes/l10n.php` |

Retrieves the translation of $text and escapes it for safe use in HTML output.

  | 
| [convert_smilies()](https://developer.wordpress.org/reference/functions/convert_smilies/)`wp-includes/formatting.php` |

Converts text equivalent of smilies to images.

  | 
| [shortcode_unautop()](https://developer.wordpress.org/reference/functions/shortcode_unautop/)`wp-includes/formatting.php` |

Don’t auto-p wrap shortcodes that stand alone.

  | 
| [wptexturize()](https://developer.wordpress.org/reference/functions/wptexturize/)`wp-includes/formatting.php` |

Replaces common plain text characters with formatted entities.

  | 
| [is_singular()](https://developer.wordpress.org/reference/functions/is_singular/)`wp-includes/query.php` |

Determines whether the query is for an existing single post of any post type (post, attachment, page, custom post types).

  | 
| [have_posts()](https://developer.wordpress.org/reference/functions/have_posts/)`wp-includes/query.php` |

Determines whether current WordPress query has posts to loop over.

  | 
| [the_post()](https://developer.wordpress.org/reference/functions/the_post/)`wp-includes/query.php` |

Iterate the post index in the loop.

  | 
| [WP_Embed::autoembed()](https://developer.wordpress.org/reference/classes/wp_embed/autoembed/)`wp-includes/class-wp-embed.php` |

Passes any unlinked URLs that are on their own line to [WP_Embed::shortcode()](https://developer.wordpress.org/reference/classes/wp_embed/shortcode/) for potential embedding.

  | 
| [WP_Embed::run_shortcode()](https://developer.wordpress.org/reference/classes/wp_embed/run_shortcode/)`wp-includes/class-wp-embed.php` |

Processes the shortcode.

  | 
| [do_shortcode()](https://developer.wordpress.org/reference/functions/do_shortcode/)`wp-includes/shortcodes.php` |

Searches content for shortcodes and filter shortcodes through their hooks.

  | 
| [has_action()](https://developer.wordpress.org/reference/functions/has_action/)`wp-includes/plugin.php` |

Checks if any action has been registered for a hook.

  | 
| [is_user_logged_in()](https://developer.wordpress.org/reference/functions/is_user_logged_in/)`wp-includes/pluggable.php` |

Determines whether the current visitor is a logged in user.

  |

[Show 11 more](https://developer.wordpress.org/reference/functions/get_the_block_template_html/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/get_the_block_template_html/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/get_the_block_template_html/?output_format=md#changelog)󠁿

| Version | Description | 
| [5.8.0](https://developer.wordpress.org/reference/since/5.8.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_the_block_template_html%2F)
before being able to contribute a note or feedback.