Make WordPress Core

Opened 5 years ago

Last modified 21 months ago

#23431 new feature request

[embed] shortcode doesn't work with do_shortcode()

Reported by: jtsternberg Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version: 3.5
Component: Embeds Keywords: has-patch needs-testing needs-unit-tests
Focuses: Cc:


It would be preferable to use the [embed] shortcode through do_shortcode rather than apply_filters( 'the_content',... in order to avoid sharing plugins, related posts plugins etc appending content to something that could simple be post meta, i.e. looking to convert a youtube url to a video.

Attachments (2)

23431.diff (594 bytes) - added by jtsternberg 5 years ago.
Simple check for the [embed] shortcode that allows filtering via do_shortcode()
23431.2.diff (1.2 KB) - added by kovshenin 3 years ago.

Download all attachments as: .zip

Change History (14)

#1 @helen
5 years ago

FWIW, you can do it through WP_Embed::run_shortcode().

#2 @jtsternberg
5 years ago

No, that method doesn't work because it's not a proper static method and uses the $this variable which is unavailable. I CAN do

	$embed = new WP_Embed();
	$video = $embed->run_shortcode( '[embed width="890"]'. esc_url( $video_url ) .'[/embed]' );

But that definitely seems like overkill to me. (Please correct me if I'm wrong)

Last edited 5 years ago by jtsternberg (previous) (diff)

#3 @jtsternberg
5 years ago

Ok, digging a bit further, I realized the WP_Embed class is initialized as a global variable so is accessible to me via

$GLOBALS['wp_embed']->run_shortcode( '[embed width="890"]'. esc_url( $video_url ) .'[/embed]' );

So pardon my interruption here. :)

Curious, should we document that in the codex?

Last edited 5 years ago by jtsternberg (previous) (diff)

#4 @jtsternberg
5 years ago

Ok, another thought regarding the point of this ticket originally: it seems plausible that we could still enable this featured through do_shortcode() by checking for the [embed] shortcode, and then running this method. I'll throw up a patch if I get a chance.

#5 @helen
5 years ago

Well, yes, working code would be global $wp_embed; $wp_embed->run_shortcode( $whatever );. I'd check out the docs for the method - I think you'll find them helpful.

5 years ago

Simple check for the [embed] shortcode that allows filtering via do_shortcode()

#6 @SergeyBiryukov
5 years ago

  • Component changed from General to Embeds
  • Version changed from trunk to 3.5

#7 @jtsternberg
5 years ago

  • Keywords has-patch added; needs-patch removed

#8 @wonderboymusic
3 years ago

  • Milestone changed from Awaiting Review to 4.0

3 years ago

#9 @kovshenin
3 years ago

I think we need to be careful here.

As you probably know, [embed] is a bit special, and it runs earlier than any other shortcode. The embed shortcode can actually return another shortcode as of 3.6, see wp_embed_handler_audio() in media.php which works fine with posts and the_content, but you'd need to run the result twice through do_shortcode() if you're going to do it manually.

There's also a caching mechanism for oEmbed which uses post meta to store the data. It uses the current post global, which means that if you're running the embed shortcode (or the_content filter for that matter) manually without an explicit post context (outside of the loop), you can get pretty weird stuff, like non-relevant oembed cache data stored with the last post of the main query, meaning a "cache flush" every time you publish a new post.

That said using the run_shortcode() method is overkill in this scenario, because you already know the shortcode tag and the URL you'd like to embed, so there's no reason to run the regex in do_shortcode() one more time. You can invoke the shortcode() method directly, which will happily accept shortcode attributes in standard form, and doing that is essentially the same as registering an actual (vs fake) callback for the embed shortcode, see 23431.2.diff.

This ticket was mentioned in IRC in #wordpress-dev by helen. View the logs.

3 years ago

#11 @helen
3 years ago

  • Milestone changed from 4.0 to Future Release

#12 @chriscct7
21 months ago

  • Keywords needs-testing needs-unit-tests added
Note: See TracTickets for help on using tickets.