Remove Width & Height Attributes in WordPress

When you upload an image through the WordPress media uploader and then insert it into the editor, it comes with width and height attributes. These are normally desirable, as it assists the browser in making the appropriate room for the image during layout. But if you want to remove the insert action from adding these attributes, you can add this code to your functions.php file or a functionality plugin of your own making:

add_filter( 'post_thumbnail_html', 'remove_width_attribute', 10 );
add_filter( 'image_send_to_editor', 'remove_width_attribute', 10 );

function remove_width_attribute( $html ) {
   $html = preg_replace( '/(width|height)="d*"s/', "", $html );
   return $html;

Update: this is almost certainly a bad idea nowdays, since width and height help reserve space for the image while loading, even in fluid-width situations.

See also  Change Address Checkout Field Into Drop-down

Leave a Reply