Make WordPress Core


Ignore:
Timestamp:
11/04/2025 12:45:02 AM (4 months ago)
Author:
westonruter
Message:

Embeds: Add wp_oembed_add_discovery_links() to run earlier at wp_head priority 4.

This results in the oEmbed discovery links being printed before scripts and styles are printed. This helps ensure they appear within the first 150K bytes of the HTML response, which is required by WP_oEmbed::discover(). With increasing the styles_inline_size_limit from 20K to 40K in #63018, it becomes more probable that the oEmbed discovery links will be pushed out of range.

For backwards compatibility with themes and plugins that disable the oEmbed discovery links by unhooking wp_oembed_add_discovery_links() from running at wp_head priority 10 (even though the oembed_discovery_links filter is available to disable such links), this callback is added a second time to run earlier at priority 4. The first time the function runs, it checks to see if the callback is still hooked at priority 10. If not, the function short circuits. If it is still hooked at priority 10, however, the function then unhooks itself at priority 10 so that it won't run a second time later during the wp_head action, before proceeding with printing the discovery links. A similar back-compat approach was taken previously in [60910]. The back-compat checks are only performed if the function is invoked during the wp_head action, allowing the function to be called "idempotently" elsewhere.

Developed in https://github.com/WordPress/wordpress-develop/pull/10449

Follow-up to [61118], [61117].

Props westonruter, swissspidy.
See #64186, #63018.
Fixes #64178.

File:
1 edited

Legend:

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

    r60948 r61119  
    333333 * @since 4.4.0
    334334 * @since 6.8.0 Output was adjusted to only embed if the post supports it.
     335 * @since 6.9.0 Now runs first at `wp_head` priority 4, with a fallback to priority 10. This helps ensure the discovery links appear within the first 150KB.
    335336 */
    336337function wp_oembed_add_discovery_links() {
     338    if ( doing_action( 'wp_head' ) ) {
     339        // For back-compat, short-circuit if a plugin has removed the action at the original priority.
     340        if ( ! has_action( 'wp_head', 'wp_oembed_add_discovery_links', 10 ) ) {
     341            return;
     342        }
     343
     344        // Prevent running again at the original priority.
     345        remove_action( 'wp_head', 'wp_oembed_add_discovery_links' );
     346    }
     347
    337348    $output = '';
    338349
Note: See TracChangeset for help on using the changeset viewer.