[wp-trac] [WordPress Trac] #44632: Properly enqueue wp-embed

WordPress Trac noreply at wordpress.org
Thu Dec 16 23:27:42 UTC 2021

#44632: Properly enqueue wp-embed
 Reporter:  alpipego                 |       Owner:  westonruter
     Type:  enhancement              |      Status:  reopened
 Priority:  normal                   |   Milestone:  5.9
Component:  Embeds                   |     Version:  4.4
 Severity:  normal                   |  Resolution:
 Keywords:  has-patch has-unit-      |     Focuses:  javascript,
  tests                              |  performance
Changes (by westonruter):

 * status:  closed => reopened
 * resolution:  fixed =>


 It turns out that the approach taken here doesn't work with block-based
 themes like TT2, as [https://github.com/WordPress/wordpress-
 develop/pull/1745#issuecomment-995560177 discovered] by @kafleg. Given
 this old action code:

 add_action( 'wp_head', 'wp_oembed_add_host_js' );

 The changes introduced in this ticket involve making
 `wp_oembed_add_host_js()` do simply:

 add_filter( 'embed_oembed_html', 'wp_maybe_enqueue_oembed_host_js' );

 The problem is that with block-based themes, `do_blocks()` runs before
 `wp_head` runs when `get_the_block_template_html()` is called. This means
 that the `embed_oembed_html` filter would have already applied run before
 `wp_head` is done, as seen in [https://github.com/WordPress/wordpress-
 develop/blob/trunk/src/wp-includes/template-canvas.php template-

  * Get the template HTML.
  * This needs to run before <head> so that blocks can add scripts and
 styles in wp_head().
 $template_html = get_the_block_template_html();
 ?><!DOCTYPE html>
 <html <?php language_attributes(); ?>>
         <meta charset="<?php bloginfo( 'charset' ); ?>" />
         <?php wp_head(); ?>

 <body <?php body_class(); ?>>
 <?php wp_body_open(); ?>

 <?php echo $template_html; // phpcs:ignore WordPress.Security.EscapeOutput

 <?php wp_footer(); ?>

 If we want to maintain back-compat for allowing authors to disable the
 oEmbed host JS via:

 remove_action( 'wp_head', 'wp_oembed_add_host_js' )

 Then I think what we need to do is something like this:

Ticket URL: <https://core.trac.wordpress.org/ticket/44632#comment:47>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform

More information about the wp-trac mailing list