WordPress.org

Make WordPress Core

Opened 17 months ago

Last modified 17 months ago

#37183 new defect (bug)

Nested shortcodes in new-style [caption]

Reported by: pputzer Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 3.4
Component: Shortcodes Keywords: has-patch
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 17 months ago.
New img_caption_shortcode_content filter
37183.2.patch (1.6 KB) - added by pputzer 17 months ago.
Removed package.json

Download all attachments as: .zip

Change History (4)

@pputzer
17 months ago

New img_caption_shortcode_content filter

#1 @pputzer
17 months ago

  • Keywords has-patch added

#2 @tychay
17 months ago

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

@pputzer
17 months ago

Removed package.json

Note: See TracTickets for help on using tickets.