Want to make a new responsive WordPress theme and need to remove the automatically generated width and height attributes? For example, WordPress is generating image code like this:

Image

When what you need is:

Image

Exactly the same, sans the width and height attributes. Well, here’s how (filter info from here)

1. Open your theme’s functions.php (e.g. wp-content/themes/*your theme*/functions.php
2. Add the following filter:

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

This will remove the inline width and height attributes for thumbnails and images added through the editor, however, it won’t remove them from images retrieved through wp_get_attachment_image or other related functions (as there are no hooks in there).

My latest book, 'Enduring CSS' is out now

Get $5 off HERE ↠

Write and maintain large scale modular CSS and embrace modern tooling including PostCSS, Stylelint and Gulp