WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] caption shortcode is not adding the style and width (4 posts)

  1. lelandmcf
    Member
    Posted 4 years ago #

    I am developing a site and when I add an image with a caption, the width of the caption does not get transferred to the div that is created for the caption.
    Ex.
    [caption id="attachment_192" align="alignleft" width="147" caption="Caption Text"]<img class="size-thumbnail wp-image-192" title="Image Name" src="http://example.com/wp-content/uploads/2009/03/img.jpg" alt="name" width="147" height="150" />[/caption]

    produces
    <div class="wp-caption alignleft" id="attachment_192"><img height="150" width="147" alt="Image Name" src="http://example.com/wp-content/uploads/2009/03/img.jpg" title="Image Name" class="size-thumbnail wp-image-192"><p class="wp-caption-text">Caption Text</p></div>
    Note the missing style="width:147px;" in the wp-caption div. Any help would be appreciated.

  2. esmi
    Forum Moderator
    Posted 4 years ago #

    Have you tried:

    - deactivating all plugins to see if this resolves the problem? If this works, re-activate the plugins one by one until you find the problematic plugin(s).

    - switching to the default theme to rule out any theme-related problems?

    I've known plugins interfere with caption shortcode parsing.

  3. lelandmcf
    Member
    Posted 4 years ago #

    Ok, I figured it out. WordPress did not place a ";" after the css and the browser disregarded it all I did was added this PHP code to functions.php:

    function fb_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="' . $id . '" ';
    
    	return '<div ' . $id . 'class="caption wp-caption ' . $align . '" style="width: ' . (10 + (int) $width) . 'px;">'
    	. do_shortcode( $content ) . '<p class="wp-caption-text">' . $caption . '</p></div>';
    }
    
    add_shortcode('wp_caption', 'fb_img_caption_shortcode');
    add_shortcode('caption', 'fb_img_caption_shortcode');
  4. ywickham
    Member
    Posted 4 years ago #

    Esmi -

    Your suggestion to deactivate all the plug-ins to see if that resolved the problem worked for me. I was able to narrow down the trouble to a faulty plug-in. Once removed, my captions came back.

    Thank you!

Topic Closed

This topic has been closed to new replies.

About this Topic