0

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.

  • 1
    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
  • 2
    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
  • 2
    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. – Nicolai Jan 23 '14 at 14:13

Your Answer

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

Browse other questions tagged or ask your own question.