2

I'm using a snippet of code to effectively remove the 10px of added width Wordpress adds to caption images on the front end of the site. However, it's still adding 10px in the tinymce editor:

<dl id="attachment_69" class="wp-caption alignleft" style="width: 310px" data-mce-style="width: 310px;">

How can I remove this 10px? I've tried modifying the core (bad idea, I know) by changing "10 +" to 0 in a few places, but I'm not having any luck.

Thanks!

1 Answer 1

6

Here's what we're using. Put this in the functions.php for your theme.

// Override img caption shortcode to fix 10px issue.
add_filter('img_caption_shortcode', 'fix_img_caption_shortcode', 10, 3);

function fix_img_caption_shortcode($val, $attr, $content = null) {
    extract(shortcode_atts(array(
        'id'    => '',
        'align' => '',
        'width' => '',
        'caption' => ''
    ), $attr));

    if ( 1 > (int) $width || empty($caption) ) return $val;

    return '<div id="' . $id . '" class="wp-caption ' . esc_attr($align) . '" style="width: ' . (0 + (int) $width) . 'px">' . do_shortcode( $content ) . '<p class="wp-caption-text">' . $caption . '</p></div>';
}

This eliminates the 10px problem altogether and removes the need for workarounds.

2
  • 1
    Thanks for the snipped I just changed the width to auto because I didn't want any width. But why in the first place do I even need a "workaround", why is there even such a "feature" that adds a fixed width to the image?
    – Yves
    Nov 13, 2014 at 11:23
  • Couldn't agree more, Yves. I don't understand it. Seems like really poor form.
    – Shane Pike
    Apr 23, 2015 at 20:09

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.