Make WordPress Core

Opened 7 years ago

Closed 7 years ago

#40926 closed defect (bug) (duplicate)

the_content filter does not run wp_oembed_get() unless there is a global post

Reported by: gungeekatx's profile GunGeekATX Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.7.5
Component: Embeds Keywords:
Focuses: Cc:

Description

While working through a custom REST API endpoint, I found that the_content filter does not process oEmbeds unless there is a global post set. In class WP_Embed, down in the shortcode() function, it first checks for known handlers, then passes it off to wp_oembed_get(), but only first after checking for a cached oEmbed for the current post. If there is no global post currently set, the oEmbed processing does not happen.

Theoretically, calling something like this should create the oEmbed HTML for Matt's State of the World 2016 video.

$html = apply_filters( 'the_content', 'https://www.youtube.com/watch?v=Nl6U7UotA-M' );

But if there is no global post set, it just returns it in <p> tags

<p>https://www.youtube.com/watch?v=Nl6U7UotA-M</p>

In my case, since I was working with posts, I was able to call setup_postdata() before creating the custom response to get around this. In cases where there isn't a global post, such as content stored in the options table, something global for a theme, content fetched from an external source, etc., it would be handy to still be able to process oEmbeds.

Not exactly sure the best way to refactor that bit of code would be. Allowing these to be cached as a transient when there is no global post would fix the issue, but that could create a lot of outdated rows in the options table.

Change History (1)

#1 @swissspidy
7 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #34115.

Note: See TracTickets for help on using tickets.