Make WordPress Core

Opened 8 years ago

Last modified 4 years ago

#37183 new defect (bug)

Nested shortcodes in new-style [caption]

Reported by: pputzer's profile pputzer Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.4
Component: Shortcodes Keywords: has-patch dev-feedback
Focuses: Cc:

Description

Splitting this of from #24990 after discussions on WCEU contributor day:

Having something like [caption][shortcode]<a href=""><img></a>[/shortcode] Caption Text[/caption] does not work. The opening shortcode get's thrown out completely because caption only begins to parse at <a .... The problem lies in the regex line

if ( preg_match( '#((?:<a [^>]+>\s*)?<img [^>]+>(?:\s*</a>)?)(.*)#is', $content, $matches ) ) {

that throws out any shortcode enclosing the image and/or link tag in the $content.

Currently, the only workaround is to replace wp_caption and caption entirely, like this:

function media_credit_caption_shortcode($attr, $content = null) {
    // New-style shortcode with the caption inside the shortcode with the link and image tags.
    if ( ! isset( $attr['caption'] ) ) {
	 if ( preg_match( '#((?:\[media-credit[^\]]+\]\s*)(?:<a [^>]+>\s*)?<img [^>]+>(?:\s*</a>)?(?:\s*\[/media-credit\])?)(.*)#is', $content, $matches ) ) {
	     $content = $matches[1];
	     $attr['caption'] = trim( $matches[2] );
	 }
     }
	       
     return img_caption_shortcode($attr, $content);
}
	
add_shortcode('wp_caption', 'media_credit_caption_shortcode');
add_shortcode('caption', 'media_credit_caption_shortcode');	

The regex can't be removed entirely because it converts the new-style caption syntax introduced in WordPress 3.4 to the older one used internally. Following a suggestion from @tychay, I've come with the attached patch filtering the $matches instead of the regex itself.

The proposed filter would also enable new-style captions for media elements other than <img> (which currently are only supported if you use the old-style attribute syntax). The default behavior is not changed, though. Currently existing unit tests are not affected.

Attachments (2)

37183.patch (2.0 KB) - added by pputzer 8 years ago.
New img_caption_shortcode_content filter
37183.2.patch (1.6 KB) - added by pputzer 8 years ago.
Removed package.json

Download all attachments as: .zip

Change History (6)

@pputzer
8 years ago

New img_caption_shortcode_content filter

#1 @pputzer
8 years ago

  • Keywords has-patch added

#2 @tychay
8 years ago

Nice one. I forgot I even mentioned this. :-)

@pputzer
8 years ago

Removed package.json

#3 @pputzer
5 years ago

  • Keywords dev-feedback added

Anyone want to look at this?

#4 @pputzer
4 years ago

I know that shortcodes are not "sexy" anymore, but a quick review before I do a refresh would be nice.

Note: See TracTickets for help on using tickets.