WordPress.org

Make WordPress Core

Opened 8 years ago

Closed 7 years ago

Last modified 6 years ago

#20740 closed defect (bug) (worksforme)

strip_shortcodes Chokes On Empty Shortcodes

Reported by: MathSmath Owned by:
Milestone: Priority: normal
Severity: minor Version: 3.3.2
Component: Shortcodes Keywords:
Focuses: Cc:

Description

It appears the regex for strip_shortcodes doesn't recognize empty shortcodes. For example, a caption shortcode like this would only have the opening shortcode tag stripped:

[caption id="attachment_3360" width="288" caption="Some caption"][/caption]

The closing caption tag remains in the resulting string.

I realize this is a fringe case (why would you have an empty caption?), but it popped up for me when working with a popular plugin that attempts to generate an excerpt by doing the following:

$content = strip_tags($content);
$content = strip_shortcodes($content);

Seems harmless--but if a caption contains only an image tag, the strip_tags removes it, and the strip_shortcodes chokes on the now-empty shortcode. Or even worse, if you have a second caption shortcode in the content, it strips EVERYTHING between the first opening caption tag and the second closing caption tag.

And I have to imagine there are some custom shortcodes that may be legitimately empty for some reason. I dunno.

To reproduce:

$content = '[caption][/caption]

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

[caption][/caption]';

$content = strip_shortcodes($content);

echo $content;

Change History (4)

#1 @iseulde
7 years ago

  • Component changed from General to Shortcodes

#2 @ericlewis
7 years ago

  • Resolution set to worksforme
  • Status changed from new to closed

Can not reproduce with either of these code samples:

<?php
$content = '[caption id="attachment_3360" width="288" caption="Some caption"][/caption]';
$content = strip_shortcodes($content);
var_dump( $content ); // result: string(0)
<?php
$content = '[caption][/caption]
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
[caption][/caption]';
$content = strip_shortcodes($content);
var_dump( $content ); // result: string(60) " Lorem ipsum dolor sit amet, consectetur adipiscing elit. "

Closing out.

Last edited 7 years ago by ericlewis (previous) (diff)

#3 @markoheijnen
7 years ago

  • Milestone Awaiting Review deleted

Clearing out the milestones for closed tickets on Awaiting Review

#4 @MathSmath
6 years ago

Weird--I can't remember what project I was working on when I reported this, but now I can't reproduce it either. Must have had to do with a plugin something.

My bad! Thanks for validating!

Note: See TracTickets for help on using tickets.