Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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:

[caption id="attachment_40" align="alignleft" width="1024"]<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[/caption]

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.:

[caption id="attachment_40" align="alignleft" width="1024" caption="That's what I get for only spending $15 on a motorcycle cover"]<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>[/caption]

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?

share|improve this question

1 Answer

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...

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.