Nested Shortcode Inside [caption] Doesn’t Process

Captions in wordpress do not support nested shortcodes at the moment (v3.6). So, If I do write

<img src=""> I love my [city]

Where city is suppose to be processed but it does not. How do I fix this?

Ticket: #24990

3 Answers
3

There is a hook inside the caption shortcode that will allow you to hijack the whole thing. Most of the following is copied from the Core img_caption_shortcode function.

function nested_img_caption_shortcode($nada, $attr, $content = null) {

  extract(
    shortcode_atts(
      array(
      'id'    => '',
      'align' => 'alignnone',
      'width' => '',
      'caption' => ''
      ), 
      $attr, 
      'caption'
    )
  );

  $caption = do_shortcode($caption); // process nested shortcodes

  if ( 1 > (int) $width || empty($caption) )
          return $content;

  if ( $id ) $id = 'id="' . esc_attr($id) . '" ';

  return '<div ' . $id . 'class="wp-caption ' . esc_attr($align) . '" style="width: ' . (10 + (int) $width) . 'px">'
  . do_shortcode( $content ) . '<p class="wp-caption-text">' . $caption . '</p></div>';
}
add_filter('img_caption_shortcode', 'nested_img_caption_shortcode', 1, 3);

Leave a Comment