Take the 2-minute tour ×
WordPress Development Stack Exchange 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
add comment

3 Answers

up vote 1 down vote accepted

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.

share|improve this answer
    
Please add an explanation to your answer: why could that solve the problem? –  toscho Aug 9 '13 at 6:00
    
This change applies to the "functions.php" in "\bonpress\functions\theme\functions.php". There is a comment above the code that reads "Remove [caption] in-line styling". This is not what you want to do so the code should be commented out. –  Ken Shoufer Aug 10 '13 at 12:07
add comment

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
add comment

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.

share|improve this answer
add comment

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.