WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 4 years ago

#32208 closed defect (bug) (invalid)

apply_filters('the_content') not working as expected in some cases

Reported by: zabatonni Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.2.1
Component: Embeds Keywords:
Focuses: Cc:

Description

When I use this filter for example in widget area for displaying text with video embed it works on most urls, but in some cases it won't make embed out of url.

Tried it on fresh install without plugins using:

echo apply_filters('the_content','https://www.youtube.com/watch?v=NwJ7tfVj1pI');

I found that this is happening on urls where are no posts, like empty categories, e404, etc... even home without posts.

On these pages there is no $post and thats why its not filtering text as it should.

Change History (4)

#1 @SergeyBiryukov
4 years ago

  • Component changed from Formatting to Embeds
  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

You should use wp_oembed_get() directly:

echo wp_oembed_get( 'https://www.youtube.com/watch?v=NwJ7tfVj1pI' );

#2 @zabatonni
4 years ago

  • Resolution invalid deleted
  • Status changed from closed to reopened

please.. i know i can convert single url to embed, i'm talking about text in widget like THIS:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam euismod volutpat mi at molestie. Duis auctor nulla iaculis, auctor risus mattis, condimentum urna. Suspendisse aliquet leo libero, iaculis aliquam sapien rutrum at. Vivamus eu auctor lorem, id scelerisque ante. 

YOUTUBE_URL

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam euismod volutpat mi at molestie. Duis auctor nulla iaculis, auctor risus mattis, condimentum urna. Suspendisse aliquet leo libero, iaculis aliquam sapien rutrum at. Vivamus eu auctor lorem, id scelerisque ante.

[gallery]

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam euismod volutpat mi at molestie. Duis auctor nulla iaculis, auctor risus mattis, condimentum urna. Suspendisse aliquet leo libero, iaculis aliquam sapien rutrum at. Vivamus eu auctor lorem, id scelerisque ante.

#3 @SergeyBiryukov
4 years ago

  • Milestone set to Awaiting Review

#4 @Viper007Bond
4 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from reopened to closed

@SergeyBiryukov is correct. WP_Embed, the class which is hooked onto the the_content filter, caches oEmbed results to the post's meta and is not designed to be used outside of post types (posts, pages, etc.).

Even if it does work in some cases, it's doing extra HTTP requests and spamming the post meta of whatever was the last post to be displayed on the page due to the $post global.

If you wish to use it inside of a widget, then you need to use the lower level wp_oembed_get() function and implement your own caching to avoid doing a HTTP request on each page load.

Note: See TracTickets for help on using tickets.