Mar 27, 2013
Remove automatic dimensions from WordPress Image attachments
WordPress Thumbnail function when used in themes, automatically adds width and height attributes to the image attachments, which may create problems with a responsive layout. Take a look at the highlighted section in the below given code, that’s what I’m talking about:
In such a situation, WordPress developers want those highlighted, hard-coded width & height attributes snipped from the image attachments. It can be achieved by adding a few lines of code to the functions.php of our theme. Grab the code below, paste it in your functions.php and save the changes:
function remove_thumbnail_dimensions( $html, $post_id, $post_image_id ) {
$html = preg_replace( '/(width|height)="d*"s/', "", $html );
return $html;}
add_filter( 'post_thumbnail_html', 'remove_thumbnail_dimensions', 10, 3 );
After saving the changes, you will receive image attachments without height and width attributes in the markup. Hope you found it useful. Thanks :)