12 February 2014

Remove Height & Width Attributes From Images In Wordpress

WordPress will automatically add the image Width and Height attribute in the image element.

If you want to remove height and width attributes from the image elements it can be done with the following filters:-
  • post_thumbnail_html - Filter any post thumbnails.
  • image_send_to_editor - Filter the HTML when adding a image to the editor.

Add this code into your theme's `functions.php` file.

add_filter( 'image_send_to_editor', 'remove_attribute_from_image', 10 );
add_filter( 'post_thumbnail_html', 'remove_attribute_from_image', 10 );
add_filter( 'get_image_tag', 'remove_attribute_from_image', 10 );

function remove_attribute_from_image( $html )
{
 return preg_replace( '/(width|height)="\d*"\s/', "", $html );
}

Have Fun!

No comments:

Post a Comment