WordPress.org

Make WordPress Core


Ignore:
Timestamp:
01/04/2022 03:22:40 PM (2 weeks ago)
Author:
audrasjb
Message:

Embeds: Fix oEmbed host script enqueueing on block-based themes.

This change fixes oEmbed host script enqueueing on front-end when using block themes.

It deprecates wp_oembed_add_host_js in favor of wp_maybe_enqueue_oembed_host_js. The action is still triggered in default-filters.php to ensure backward compatibility for websites that are removing the action. There is now a has_action() check in wp_maybe_enqueue_oembed_host_js() to see if wp_oembed_add_host_js() has not been unhooked from running at the wp_head action.

Follow-up to [52132], [52151], [52153], [52325].

Props swissspidy, westonruter, flixos90, kafleg.
Fixes #44632.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/embed.php

    r52332 r52437  
    357357 * Adds the necessary JavaScript to communicate with the embedded iframes.
    358358 *
    359  * @since 4.4.0
    360  */
    361 function wp_oembed_add_host_js() {
    362     add_filter( 'embed_oembed_html', 'wp_maybe_enqueue_oembed_host_js' );
    363 }
     359 * This function is no longer used directly. For back-compat it exists exclusively as a way to indicate that the oEmbed
     360 * host JS _should_ be added. In `default-filters.php` there remains this code:
     361 *
     362 *     add_action( 'wp_head', 'wp_oembed_add_host_js' )
     363 *
     364 * Historically a site has been able to disable adding the oEmbed host script by doing:
     365 *
     366 *     remove_action( 'wp_head', 'wp_oembed_add_host_js' )
     367 *
     368 * In order to ensure that such code still works as expected, this function remains. There is now a `has_action()` check
     369 * in `wp_maybe_enqueue_oembed_host_js()` to see if `wp_oembed_add_host_js()` has not been unhooked from running at the
     370 * `wp_head` action.
     371 *
     372 * @since 4.4.0
     373 * @deprecated 5.9.0 Use {@see wp_maybe_enqueue_oembed_host_js()} instead.
     374 */
     375function wp_oembed_add_host_js() {}
    364376
    365377/**
     
    375387 */
    376388function wp_maybe_enqueue_oembed_host_js( $html ) {
    377     if ( preg_match( '/<blockquote\s[^>]*?wp-embedded-content/', $html ) ) {
     389    if (
     390        has_action( 'wp_head', 'wp_oembed_add_host_js' )
     391        &&
     392        preg_match( '/<blockquote\s[^>]*?wp-embedded-content/', $html )
     393    ) {
    378394        wp_enqueue_script( 'wp-embed' );
    379395    }
Note: See TracChangeset for help on using the changeset viewer.