goranseric
1/17/2013 - 5:47 PM

Slightly smarter filtering to remove hard-coded width and height attributes from *all* images in WordPress (post thumbnails, images inserted

Slightly smarter filtering to remove hard-coded width and height attributes from all images in WordPress (post thumbnails, images inserted into posts, and gravatars). Handy for responsive designs. Add the code below to the functions.php file in your theme's folder (/wp-content/themes/theme-name/ ). Remember to rename the function as needed to match your theme name so you can avoid any namespace issues.

Based on: http://css-tricks.com/snippets/wordpress/remove-width-and-height-attributes-from-inserted-images/ And: http://css-tricks.com/snippets/wordpress/remove-width-and-height-attributes-from-inserted-images/

/**
* Filter out hard-coded width, height attributes on all images in WordPress. 
* https://gist.github.com/4557917
*
* This version applies the function as a filter to the_content rather than send_to_editor. 
* Changes made by filtering send_to_editor will be lost if you update the image or associated post 
* and you will slowly lose your grip on sanity if you don't know to keep an eye out for it. 
* the_content applies to the content of a post after it is retrieved from the database and is "theme-safe". 
* (i.e., Your changes will not be stored permanently or impact the HTML output in other themes.)
*
* Also, the regex has been updated to catch both double and single quotes, since the output of 
* get_avatar is inconsistent with other WP image functions and uses single quotes for attributes. 
* [insert hate-stare here]
*
*/
function mytheme_remove_img_dimensions($html) {
  $html = preg_replace('/(width|height)=["\']\d*["\']\s?/', "", $html);
    return $html;
}
add_filter('post_thumbnail_html', 'mytheme_remove_img_dimensions', 10);
add_filter('the_content', 'mytheme_remove_img_dimensions', 10);
add_filter('get_avatar','mytheme_remove_img_dimensions', 10);