Opened 10 years ago
Last modified 5 years ago
#37183 new defect (bug)
Nested shortcodes in new-style [caption]
| Reported by: |
|
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.
New img_caption_shortcode_content filter