How to remove the width and height attribute from images in wordpress
16 Jun
I run into a problem with fitting big images into a single post page. I thought that just using a max-width in a CSS file to limit the image width will be enough, and the browsers will automajically scale the image to a proper dimension which will fit the available space.
I was wrong. The result was images which which where in the proper width but look stretched in their height. It took some time to figure out that the cause of it is that the height attribute set for the image with the original height.
It took some poking into the wordpress code to understand that the width and height attributes are set when the images are inserted into the editor, so I just wrote some lines of code to remove the attributes when the image is being inserted in its full size.
Too lazy to do a proper plugin out of this, but here is the code
add_filter( 'get_image_tag', 'bg_image_downsize',10,6);
function bg_image_downsize($html, $id, $alt, $title, $align, $size) {
if ($size=='full') {
$pattern = '#width="([a-zA-Z0-9]+)"#';
$html = preg_replace($pattern, '', $html);
$pattern = '#height="([a-zA-Z0-9]+)"#';
$html = preg_replace($pattern, '', $html);
}
return $html;
}
