Caption shortcodes not including caption as attribute

I recently installed the bonpress theme and am having trouble with the captions showing correctly. The more I try to understand how captions are supposed to work in WordPress, the more I suspect something is not happening right with my installation.

When creating a post and inserting an image with a caption, WordPress creates the following shortcode:

Read More
<a href="http://wordpress.local/wp-content/uploads/2012/10/2012-10-30-11.38.36.jpg"><img class="size-large wp-image-40" title="Torn motorcycle cover" src="http://wordpress.local/wp-content/uploads/2012/10/2012-10-30-11.38.36-e1351653272372-1024x984.jpg" alt="" width="1024" height="984" /></a> That's what I get for only spending $15 on a motorcycle cover

As I read through several pages such as the first example here and the sample code here (used by bonpress), it appears as though the caption should be passed as an attribute, e.g.:

<a href="http://wordpress.local/wp-content/uploads/2012/10/2012-10-30-11.38.36.jpg"><img class="size-large wp-image-40" title="Torn motorcycle cover" src="http://wordpress.local/wp-content/uploads/2012/10/2012-10-30-11.38.36-e1351653272372-1024x984.jpg" alt="" width="1024" height="984" /></a>
That's what I get for only spending $15 on a motorcycle cover

If I manually enter the shortcode with caption as an attribute, it renders correctly. Is something broken in my install that makes it so it doesn’t build it this way?

Update: The caption shortcode is constructed by image_add_caption in /wp-admin/includes/media.php (line 134 in WP 3.4.2). The shortcodes are being constructed properly according to this code, so this leaves me wondering why all the code I can find regarding the customization of img_caption_shortcode filter assumes the caption is an attribute?

Related posts

Leave a Reply

3 comments

  1. Find the line /* Remove [caption] in-line styling in function.php.

    Comment out:

    /*
    add_shortcode('wp_caption', 'fixed_img_caption_shortcode');
    add_shortcode('caption', 'fixed_img_caption_shortcode');
    function fixed_img_caption_shortcode($attr, $content = null) {
        // Allow plugins/themes to override the default caption template.
        $output = apply_filters('img_caption_shortcode', '', $attr, $content);
        if ( $output != '' ) return $output;
        extract(shortcode_atts(array(
            'id'=> '',
            'align' => 'alignnone',
            'width' => '',
            'caption' => ''), $attr));
        if ( 1 > (int) $width || empty($caption) )
        return $content;
        if ( $id ) $id = 'id="' . esc_attr($id) . '" ';
        return '<div ' . $id . 'class="wp-caption ' . esc_attr($align)
        . '">'
        . do_shortcode( $content ) . '<p class="wp-caption-text">'
        . $caption . '</p></div>';
    }    
    
    */
    

    Find .entry .wp-caption-text in style.css. Change the style to suite your taste.

    Problem fixed. You are done.

  2. I found an imperfect solution on here. Adding the following lines to the theme’s fixed_image_caption_shortcode function (previously identical to the one here) renders the captions as intended, but doesn’t add the caption as an attribute.

    if ( ! isset( $attr['caption'] ) ) {
        if ( preg_match( '#((?:<a [^>]+>s*)?<img [^>]+>(?:s*</a>)?)(.*)#is', $content, $matches ) ) {
            $content = $matches[1];
            $attr['caption'] = trim( $matches[2] );
        }
    }
    

    This solution is sufficient, but hopefully someone has a better solution…

  3. You should be able to force the shortcode back to default– to re-hijack it– by adding these two lines after that code runs:

    add_shortcode('wp_caption', 'img_caption_shortcode');
    add_shortcode('caption', 'img_caption_shortcode');
    

    That should set the shortcode back to the Core callback and undo whatever things that fixed_img_caption_shortcode broke.