Make WordPress Core

Opened 10 years ago

Last modified 5 years ago

#25820 new defect (bug)

[embed] shortcode can't be escaped like others

Reported by: madtownlems's profile MadtownLems Owned by:
Milestone: Priority: normal
Severity: minor Version:
Component: Shortcodes Keywords: roadmap
Focuses: Cc:

Description (last modified by SergeyBiryukov)

WordPress allows the use of double brackets to escape shortcodes, so that entering [[foo]] in your page or post displays [foo]. This works great for most shortcodes, and is used in the following way on enclosed shortcodes: [[foo]Hello![/foo]]

However, this feature fails when trying to use it with core's 'embed' shortcode.

To replicate:

Enter the following in a page or post:
[[embed]http://www.youtube.com/watch?v=YLO7tCdBVrA[/embed]]

What I expect to see:
[embed]http://www.youtube.com/watch?v=YLO7tCdBVrA[/embed]

What I really see:
(null)

(Apologies for the weird mix of [, ], and special characters here. had a hard time with getting the markup to render as I wanted.)

Attachments (1)

25820.diff (1.5 KB) - added by aaroncampbell 9 years ago.

Download all attachments as: .zip

Change History (12)

#1 @SergeyBiryukov
10 years ago

  • Description modified (diff)

#2 @SergeyBiryukov
10 years ago

As a workaround, this works for me in Text mode:

[embed]http://www.youtube.com/watch?v=YLO7tCdBVrA[/embed]
Last edited 10 years ago by SergeyBiryukov (previous) (diff)

#3 @MadtownLems
10 years ago

Ah yes, I should've proactively talked about that workaround. Thanks for reminding me.

That workaround has the following issue:

If you return to the Visual tab, those entities are converted back into [ and ], causing your shortcode to ultimately be processed.

#4 @pavelevap
10 years ago

Related #12760 ?

#6 @wonderboymusic
10 years ago

  • Keywords needs-patch added
  • Milestone changed from Awaiting Review to Future Release

@aaroncampbell
9 years ago

#7 @aaroncampbell
9 years ago

  • Keywords has-patch 2nd-opinion added; needs-patch removed

The problem is that the embed shortcode needs to run earlier than the other shortcodes, so it does something hacky. It hooks into the_content at priority 8 (before wpautop() runs), removes all shortcodes, adds the embed shortcode, runs it, then puts all the other shortcodes back, registering __return_false() as the handler for the embed shortcode.

So here's what's actually happening:
1) do_shortcode() is running on the_content, unescaping [[embed]someurl[/embed]] to [embed]someurl[/embed]
2) Shortcodes are processed and the embed shortcode has been unescaped so it's now a valid shortcode. However, the handler for it is __return_false() so it's simply removed.

You can see this pretty clearly by using three square brackets instead of two to escape the embed shortcode ([[[embed]someurl[/embed]]]). One set is removed during step one above and then step two processes it as an escaped shortcode, removing another one.

I looked at several potential solutions. Several of them had me making tweaks to the regex or do_shortcode() that were ridiculous. Taking a look at it again, I'm thinking it might be easier to just add another simple hack to work around our existing hack (I know, I know, stick with me for a minute). If we add a filter in do_shortcode_tag() that filters the returned text of an escaped tag, we could hook into that filter before we process the embed shortcode early, have it add the removed brackets back, then remove the hook after we're done.

That's what I did in 25820.diff. It's definitely a bit of a hack, but it's simple, straightforward, and it seems to give the expected behavior. If anyone has a better idea, let me know!

This ticket was mentioned in Slack in #core by aaroncampbell. View the logs.


9 years ago

#9 @miqrogroove
9 years ago

Does anyone actually have a reason for running embed before the other shortcodes? Why are we doing that?

#10 @miqrogroove
9 years ago

  • Keywords roadmap added; has-patch 2nd-opinion removed

In following up to this ticket, I learned that the only reason we run embeds early is to make sure their output gets into the post content before running wpautop() and wptexturize().

I'm working on a roadmap proposal that will include running all core shortcodes early for new posts. This means new embed codes will only run once instead of twice. Any old-style shortcode escaping would remain bugged for embeds and probably stay that way due to the lack of traction here.

This ticket was mentioned in Slack in #core-editor by mikevanlohuizen. View the logs.


8 years ago

Note: See TracTickets for help on using tickets.