Nested Shortcode Inside [caption] Doesn’t Process

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