Skip to content

Conversation

@westonruter
Copy link
Member

@westonruter westonruter commented Nov 2, 2025

This addresses an issue whereby if there is a lot of markup printed at wp_head (e.g. inline styles from Core-63018), WordPress sites looking for the oEmbed discovery links will fail to find them because they only read the first 150KB of the HTML response, by default:

'limit_response_size' => 153600, // 150 KB

Note that scripts and styles are printed at priorities 8 and 9:

add_action( 'wp_head', 'wp_print_styles', 8 );
add_action( 'wp_head', 'wp_print_head_scripts', 9 );

In order to ensure the oEmbed links remain discoverable, it seems prudent to print them early, specifically right after other similar links are printed, namely feed_links() and feed_links_extra().

It is not so simple as changing the action priority from 10 to 4:

add_action( 'wp_head', 'wp_oembed_add_discovery_links' );

This is because there are many plugins which remove the oEmbed discovery links via:

remove_action( 'wp_head', 'wp_oembed_add_discovery_links' );

See GitHub Search (1.7k files) and WPDirectory (145 plugins).

Aside: The better way to do this would be to do:

add_filter( 'oembed_discovery_links', '__return_empty_string' );

In order to maintain backwards-compatibility with the ecosystem which unhooks the action in this way, it is necessary to add a second instance of wp_oembed_add_discovery_links running at wp_head with the earlier priority of 4. Inside of wp_oembed_add_discovery_links(), when it runs first at priority 4, it can check if it has been unhooked at priority 10, and if so, short-circuit. Otherwise, it can continue with printing the oEmbed discovery links, but also preventing them from being duplicated by itself doing:

remove_action( 'wp_head', 'wp_oembed_add_discovery_links' );

In order to facilitate this, it is necessary to extend the has_action()/has_filter() functions with a new $priority parameter to be able to check whether a given callback is added at a specific priority. This is something I've missed in the past. It also "feels right" given that add_action()/remove_action()/add_filter()/remove_filter() all have an optional $priority parameter. See examples on GitHub and on WP Directory where ecosystem code assumed there was such a $priority parameter. This is important because the same callback can be added multiple times on the same hook with different priorities.

A similar approach to backwards-compatibility has been done recently, with wp_print_auto_sizes_contain_css_fix() in example r60910. The difference there is that a new function was introduced, whereas here the function remains the same.

TODO

  • Add tests

Trac ticket: https://core.trac.wordpress.org/ticket/64178


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@github-actions
Copy link

github-actions bot commented Nov 2, 2025

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@westonruter westonruter marked this pull request as ready for review November 2, 2025 04:53
@github-actions
Copy link

github-actions bot commented Nov 2, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props westonruter, swissspidy.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copy link
Member

@swissspidy swissspidy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is what it takes to maintain back compat... I just hope we never have to change the priority again :-)

@github-actions

This comment was marked as off-topic.

@github-actions github-actions bot closed this Nov 3, 2025
@westonruter westonruter reopened this Nov 3, 2025
@westonruter
Copy link
Member Author

I've opened #10456 to introduce the has_filter()/has_action() change in isolation. Tests have been added there.

pento pushed a commit that referenced this pull request Nov 4, 2025
…ead` 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 #10449

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

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


git-svn-id: https://develop.svn.wordpress.org/trunk@61119 602fd350-edb4-49c9-b593-d223f7449a82
@github-actions
Copy link

github-actions bot commented Nov 4, 2025

A commit was made that fixes the Trac ticket referenced in the description of this pull request.

SVN changeset: 61119
GitHub commit: 972d21c

This PR will be closed, but please confirm the accuracy of this and reopen if there is more work to be done.

@github-actions github-actions bot closed this Nov 4, 2025
markjaquith pushed a commit to WordPress/WordPress that referenced this pull request Nov 4, 2025
…ead` 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 WordPress/wordpress-develop#10449

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

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

Built from https://develop.svn.wordpress.org/trunk@61119


git-svn-id: http://core.svn.wordpress.org/trunk@60455 1a063a9b-81f0-0310-95a4-ce76da25c4cd
github-actions bot pushed a commit to platformsh/wordpress-performance that referenced this pull request Nov 4, 2025
…ead` 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 WordPress/wordpress-develop#10449

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

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

Built from https://develop.svn.wordpress.org/trunk@61119


git-svn-id: https://core.svn.wordpress.org/trunk@60455 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants