Post Pic

How to Remove the Width and Height Attributes From WP Image Uploader

When you upload images via WordPress image uploader and insert it into your posts, WordPress automatically include the image width and height attributes in the html <img> tag. But when using a responsible theme, this can cause lots of trouble. Here’s a quick recipe to get rid of those attributes.

Simply paste the following code into your theme functions.php:

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;
}

Thanks to Damien for the cool hack!

5 Responses

May 09 2012 02:28

Nice. Thanks for the tip!

May 09 2012 03:34

if you wanted to remove the other variables too

$html = preg_replace(‘/(alt|title|class|width|height)=\”[^"]*”\s/’,”, trim($html));

May 15 2012 08:25

But keep in mind that having these attributes helps for faster loading of the images. So don’t remove them if you have no problems or reasons :) .

May 21 2012 21:55

Interesting find, thanks for sharing.

Jun 03 2012 11:12

@author thanks it will help me!

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required

WP Theme of the week

Sponsored Likebox