Skip to content

oEmbed: Fall back to XML parsing when JSON parsing fails - #12657

Open
shrivastavanolo wants to merge 5 commits into
WordPress:trunkfrom
shrivastavanolo:fix/oembed-xml-format-fallback
Open

shrivastavanolo wants to merge 5 commits into
WordPress:trunkfrom
shrivastavanolo:fix/oembed-xml-format-fallback

Conversation

@shrivastavanolo

Copy link
Copy Markdown

When WP_oEmbed::_fetch_with_format() requests format=json but JSON parsing of the response fails, this falls back to attempting to parse the same response body as XML before giving up. Some oEmbed providers advertise an XML-only endpoint via a <link rel="alternate" type="text/xml+oembed" href="..."> discovery tag.

WordPress's fetch() always appends &format=json to the request regardless of the discovered endpoint's declared type. A spec-compliant provider correctly ignores that parameter and returns a valid XML response with an HTTP 200 status.

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

Use of AI Tools

AI assistance: Yes
Tool(s): Claude
Model(s): Claude Sonnet 5
Used for: Investigating the bug and drafting the initial fix. Reviewed, debugged, and verified end-to-end by me.


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.

Copilot AI review requested due to automatic review settings July 23, 2026 11:36

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR improves resilience of WordPress’s oEmbed fetching by adding a fallback parse path when a provider returns XML despite WordPress requesting JSON (e.g., when the discovered endpoint is effectively XML-only and ignores format=json).

Changes:

  • Parse the response body using the requested format first, then fall back to XML parsing when JSON parsing fails.
  • Return the parsed result from _fetch_with_format() after attempting the fallback.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/wp-includes/class-wp-oembed.php Outdated
@github-actions

Copy link
Copy Markdown

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

  • 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.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 23, 2026 11:41

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.

Comment thread src/wp-includes/class-wp-oembed.php Outdated
Comment thread src/wp-includes/class-wp-oembed.php
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 23, 2026 16:17

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (1)

src/wp-includes/class-wp-oembed.php:656

  • This new JSON→XML fallback changes the observable behavior of WP_oEmbed::fetch() in an important edge case (JSON request returning an XML oEmbed body). There doesn’t appear to be any PHPUnit coverage for this code path today, so it would be easy to regress.

Consider adding a unit test that mocks the HTTP response to return a valid XML oEmbed document when the request URL contains format=json, and assert that WP_oEmbed::fetch() returns the parsed object (and/or that data2html() produces expected output).

		if ( false === $data && 'json' === $format && false !== stripos( $body, '<oembed' ) ) {
			$data = $this->_parse_xml( $body );
		}

Copilot AI review requested due to automatic review settings July 24, 2026 09:29

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

@shrivastavanolo
shrivastavanolo marked this pull request as ready for review July 24, 2026 09:59
@github-actions

Copy link
Copy Markdown

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 shreya0shrivastava.

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

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