WordPress.org

Make WordPress Core

Opened 2 years ago

Last modified 12 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
Focuses: Cc:

Description

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 2 years ago.
Simple check for the [embed] shortcode that allows filtering via do_shortcode()
23431.2.diff (1.2 KB) - added by kovshenin 13 months ago.

Download all attachments as: .zip

Change History (13)

comment:1 @helen2 years ago

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

comment:2 @jtsternberg2 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.

Version 0, edited 2 years ago by jtsternberg (next)

comment:3 @jtsternberg2 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 2 years ago by jtsternberg (previous) (diff)

comment:4 @jtsternberg2 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.

comment:5 @helen2 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.

@jtsternberg2 years ago

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

comment:6 @SergeyBiryukov2 years ago

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

comment:7 @jtsternberg2 years ago

  • Keywords has-patch added; needs-patch removed

comment:8 @wonderboymusic14 months ago

  • Milestone changed from Awaiting Review to 4.0

@kovshenin13 months ago

comment:9 @kovshenin13 months 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.

comment:10 @ircbot12 months ago

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

comment:11 @helen12 months ago

  • Milestone changed from 4.0 to Future Release
Note: See TracTickets for help on using tickets.