Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
1  
Hmmm... actually I could just use the CSS max-width trick, that'll supersede the element attributes. –  Ben Everard Jan 23 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 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. –  ialocin Jan 23 at 14:13

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.