[wp-trac] [WordPress Trac] #37183: Nested shortcodes in new-style [caption]

WordPress Trac noreply at wordpress.org
Sun Jun 26 13:05:27 UTC 2016


#37183: Nested shortcodes in new-style [caption]
--------------------------+-----------------------------
 Reporter:  pputzer       |      Owner:
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Shortcodes    |    Version:  3.4
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 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.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/37183>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list