X

How To Remove Width and Height Attributes From Images in Posts

Snippets by IsItWP

Do you want to remove width and height attributes from images in posts? This snippet will remove all of the width and height attributes from your images in order to allow a responsive theme to handle the sizing.

Instructions

All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:

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

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our WPtouch Review.

Comments   Leave a Reply

  1. Thank You so much.

  2. If you want to do this for responsive purposes a better way would be to resolve this issue with CSS, since CSS overwrites this attributes:

    img{
    max-width:100%;
    height: auto;
    }

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!