WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm looking to remove image dimensions from the_post_thumbnail, as well as the_content. I've found the following code, but there's a slight issue:

add_filter( 'post_thumbnail_html', 'remove_thumbnail_dimensions', 10 );
add_filter( 'image_send_to_editor', 'remove_thumbnail_dimensions', 10 );
add_filter( 'the_content', 'remove_thumbnail_dimensions', 10 );
function remove_thumbnail_dimensions( $html ) {
    $html = preg_replace( '/(width|height)=\"\d*\"\s/', "", $html );
    return $html;

The issue I have with this code is that it'll end up removing the width / height attributes from any element, this could be a problem for iframe or object elements.

What's the best way to only remove these attributes for img elements, and nothing else.

share|improve this question
Hmmm... actually I could just use the CSS max-width trick, that'll supersede the element attributes. – Ben Everard Jan 23 '14 at 12:46
Why would you need to remove the image dimensions? Perhaps if you explain the reasons in your question, somebody can give you better answers. – Otto Jan 23 '14 at 13:24
Just check for the <img>...</img> tag and conditionally remove attributes only in this cases - take a look at the first comment at this gist. I haven't tested this though, but you'll get the point from that. – ialocin Jan 23 '14 at 14:13

Your Answer


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

Browse other questions tagged or ask your own question.