WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#12505 closed defect (bug) (fixed)

embed is not registered as a shortcode

Reported by: MattyRob Owned by: Viper007Bond
Milestone: 3.0 Priority: normal
Severity: normal Version: 2.9.2
Component: Shortcodes Keywords:
Focuses: Cc:

Description

The strip_shortcodes function used regex to strip short codes from post content however it leaves [embed] short codes behind.

This is apparently because the core code does not register embed as a shortcode.

Attachments (1)

12505.patch (585 bytes) - added by Viper007Bond 5 years ago.
Register placeholder shortcode for strip_shortcode() purposes

Download all attachments as: .zip

Change History (12)

comment:1 follow-up: @belg4mit5 years ago

A work around is to add the following to functions.php

add_shortcode('embed', create_function( '', ''));

It's a no-op, which is fine because embed is actually handled specially in embed.php
(not shortcode.php). It's only purpose is to make an entry with the key embed into
the global $shortcode_tags which is what is used to strip_shortcodes.

This is especially handy if you use something like Subscribe2.

comment:2 in reply to: ↑ 1 @belg4mit5 years ago

It's a no-op, which is fine because embed is actually handled specially in embed.php

I mean media.php

comment:3 @nacin5 years ago

  • Owner set to Viper007Bond
  • Priority changed from high to normal
  • Status changed from new to assigned

comment:4 @Viper007Bond5 years ago

You guys are incorrect. "embed" is registered as a shortcode, but only for a short amount of time. Look at WP_Embed::run_shortcode() in /wp-includes/media.php. It is a the_content filter that hooks in pre-wpautop() that runs one specific shortcode.

http://www.viper007bond.com/2009/11/22/wordpress-code-earlier-shortcodes/

The only solution I can think of is to register the tag to a dummy callback (as described by belg4mit) but then substitute it out for the real callback when we actually go to render it. A bit messy, but it should work.

comment:5 follow-up: @MattyRob5 years ago

Interesting and puzzling. Why register the shortcode and then de-register it?!?

Also, if I amend your code and register 'embed' but add it to the array of tags and then strip them before restoring the original array would that 'fix' the strip_shortcodes function?

comment:6 in reply to: ↑ 5 @Viper007Bond5 years ago

Replying to MattyRob:

Interesting and puzzling. Why register the shortcode and then de-register it?!?

As I said, shortcodes normally run at 11, after wpautop() at 10. It works much better if the [embed] shortcode is processed pre-wpautop().

Also, if I amend your code and register 'embed' but add it to the array of tags and then strip them before restoring the original array would that 'fix' the strip_shortcodes function?

Not sure what you mean, but I'll be personally writing a patch to fix this very soon.

comment:7 follow-up: @MattyRob5 years ago

Thanks for the clarification - so is this (embed not being in the short codes list) needed because wpautop() somehow messes up the embed shortcode?

My 'fix' is something like this:

global $shortcode_tags;

// Backup current registered shortcodes
$orig_shortcode_tags = $shortcode_tags;

// Add 'embed' then strip all shortcodes
add_shortcode( 'embed', create_function('', '') );
$content = strip_shortcodes( $content );

// Put the original shortcodes back
$shortcode_tags = $orig_shortcode_tags;

return $content;

I guess WordPress could introduce a second strip_shortcodes function or perhaps a 'force' parameter to ensure that embed is stripped out. Looking forwar to you fix - then I can revert mine :-)

comment:8 in reply to: ↑ 7 @Viper007Bond5 years ago

Replying to MattyRob:

Thanks for the clarification - so is this (embed not being in the short codes list) needed because wpautop() somehow messes up the embed shortcode?

It has the potential to, yes, but more importantly we want the video wrapped in paragraph tags so it's not jammed up against the previous and next paragraphs. We can't just manually wrap it as we have no idea what the result will be, where the shortcode was used, etc.

@Viper007Bond5 years ago

Register placeholder shortcode for strip_shortcode() purposes

comment:9 @Viper007Bond5 years ago

Sorry, I was misread belg4mit's comment. They were correct and deserve credit for this patch.

comment:10 follow-up: @nacin5 years ago

  • Resolution set to fixed
  • Status changed from assigned to closed

(In [14035]) Register a dummy [media] shortcode for strip_shortcodes et al. props belg4mit, Viper007Bond. fixes #12505

comment:11 in reply to: ↑ 10 @Viper007Bond5 years ago

Replying to nacin:

(In [14035]) Register a dummy [media] shortcode for strip_shortcodes et al. props belg4mit, Viper007Bond. fixes #12505

It's [embed]. :P

Note: See TracTickets for help on using tickets.