diff --git a/src/wp-includes/block-template.php b/src/wp-includes/block-template.php
index ffed0e04364c0..0fc8e762c649c 100644
--- a/src/wp-includes/block-template.php
+++ b/src/wp-includes/block-template.php
@@ -207,15 +207,14 @@ function _block_template_render_title_tag() {
*
* @access private
* @since 5.8.0
+ * @since 6.4.0 Block template content now runs through a 'the_block_template_html' filter.
*
- * @global string $_wp_current_template_content
- * @global WP_Embed $wp_embed
- * @global WP_Query $wp_query
+ * @global string $_wp_current_template_content
*
* @return string Block template markup.
*/
function get_the_block_template_html() {
- global $_wp_current_template_content, $wp_embed, $wp_query;
+ global $_wp_current_template_content;
if ( ! $_wp_current_template_content ) {
if ( is_user_logged_in() ) {
@@ -224,10 +223,35 @@ function get_the_block_template_html() {
return;
}
- $content = $wp_embed->run_shortcode( $_wp_current_template_content );
- $content = $wp_embed->autoembed( $content );
- $content = shortcode_unautop( $content );
- $content = do_shortcode( $content );
+ $content = $_wp_current_template_content;
+
+ /**
+ * Filters the block template HTML content.
+ *
+ * @since 6.4.0
+ *
+ * @param string $content The entire block template HTML content.
+ */
+ $content = apply_filters( 'the_block_template_html', $content );
+ $content = str_replace( ']]>', ']]>', $content );
+
+ // Wrap block template in .wp-site-blocks to allow for specific descendant styles
+ // (e.g. `.wp-site-blocks > *`).
+ return '
' . $content . '
';
+}
+
+/**
+ * Parses dynamic blocks from the block template and re-renders them.
+ *
+ * @since 6.4.0
+ *
+ * @global WP_Query $wp_query
+ *
+ * @param string $content Block template content.
+ * @return string Updated block template content.
+ */
+function do_block_template_blocks( $content ) {
+ global $wp_query;
/*
* Most block themes omit the `core/query` and `core/post-template` blocks in their singular content templates.
@@ -246,20 +270,13 @@ function get_the_block_template_html() {
if ( is_singular() && 1 === $wp_query->post_count && have_posts() ) {
while ( have_posts() ) {
the_post();
+
$content = do_blocks( $content );
}
- } else {
- $content = do_blocks( $content );
+ return $content;
}
- $content = wptexturize( $content );
- $content = convert_smilies( $content );
- $content = wp_filter_content_tags( $content, 'template' );
- $content = str_replace( ']]>', ']]>', $content );
-
- // Wrap block template in .wp-site-blocks to allow for specific descendant styles
- // (e.g. `.wp-site-blocks > *`).
- return '' . $content . '
';
+ return do_blocks( $content );
}
/**
diff --git a/src/wp-includes/class-wp-embed.php b/src/wp-includes/class-wp-embed.php
index 6b9eaa25db7bc..58102f2dfe1b9 100644
--- a/src/wp-includes/class-wp-embed.php
+++ b/src/wp-includes/class-wp-embed.php
@@ -30,6 +30,7 @@ class WP_Embed {
*/
public function __construct() {
// Hack to get the [embed] shortcode to run before wpautop().
+ add_filter( 'the_block_template_html', array( $this, 'run_shortcode' ), 6 );
add_filter( 'the_content', array( $this, 'run_shortcode' ), 8 );
add_filter( 'widget_text_content', array( $this, 'run_shortcode' ), 8 );
add_filter( 'widget_block_content', array( $this, 'run_shortcode' ), 8 );
@@ -38,6 +39,7 @@ public function __construct() {
add_shortcode( 'embed', '__return_false' );
// Attempts to embed all URLs in a post.
+ add_filter( 'the_block_template_html', array( $this, 'autoembed' ), 6 );
add_filter( 'the_content', array( $this, 'autoembed' ), 8 );
add_filter( 'widget_text_content', array( $this, 'autoembed' ), 8 );
add_filter( 'widget_block_content', array( $this, 'autoembed' ), 8 );
diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php
index a3ea1537cb1d0..11c71d7951028 100644
--- a/src/wp-includes/default-filters.php
+++ b/src/wp-includes/default-filters.php
@@ -604,6 +604,14 @@
// Global styles custom CSS.
add_action( 'wp_enqueue_scripts', 'wp_enqueue_global_styles_custom_css' );
+// Block template.
+add_filter( 'the_block_template_html', 'shortcode_unautop', 7 );
+add_filter( 'the_block_template_html', 'do_shortcode', 8 );
+add_filter( 'the_block_template_html', 'do_block_template_blocks', 9 );
+add_filter( 'the_block_template_html', 'wptexturize' );
+add_filter( 'the_block_template_html', 'wp_filter_content_tags' );
+add_filter( 'the_block_template_html', 'convert_smilies', 20 );
+
// Block supports, and other styles parsed and stored in the Style Engine.
add_action( 'wp_enqueue_scripts', 'wp_enqueue_stored_styles' );
add_action( 'wp_footer', 'wp_enqueue_stored_styles', 1 );
diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php
index dd98b5e622b7d..ae60a23945c13 100644
--- a/src/wp-includes/media.php
+++ b/src/wp-includes/media.php
@@ -1822,6 +1822,11 @@ function wp_lazy_loading_enabled( $tag_name, $context ) {
function wp_filter_content_tags( $content, $context = null ) {
if ( null === $context ) {
$context = current_filter();
+
+ // For backward compatibility, continue using the 'template' context for the block template.
+ if ( 'the_block_template_html' === $context ) {
+ $context = 'template';
+ }
}
$add_iframe_loading_attr = wp_lazy_loading_enabled( 'iframe', $context );