Every time I add a caption to a wordpress image, wordpress decides its going to insert a hard coded style="" into the attachment html which breaks my sidebar.

enter image description here

All images without captions are inserted, perfectly normally like this (which doesn't break the theme)

enter image description here


I think you're looking for the img_caption_shortcode_width filter, within the img_caption_shortcode():

 * Filters the width of an image's caption.
 * By default, the caption is 10 pixels greater than the width of the image,
 * to prevent post content from running up against a floated image.
 * @since 3.7.0
 * @see img_caption_shortcode()
 * @param int    $width    Width of the caption in pixels. To remove this inline style,
 *                         return zero.
 * @param array  $atts     Attributes of the caption shortcode.
 * @param string $content  The image element, possibly wrapped in a hyperlink.
$caption_width = apply_filters( 'img_caption_shortcode_width', $width, $atts, $content );

So it looks like this should disable the inline style:

add_filter( 'img_caption_shortcode_width', '__return_zero' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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