WordPress Hack: Remove Width And Height Attributes From Images

Whenever you upload any image to your WordPress based website, and you use it in your post, automatically WordPress adds Width and Height attributes to all image elements (i.e. in tags).

This is fine in most of the cases, but if you want to stop WordPress from adding those attributes then you can go with this code.

You have to put this code in your functions.php file.

add_filter( 'post_thumbnail_html', 'remove_thumbnail_dimensions', 10 );
add_filter( 'image_send_to_editor', 'remove_thumbnail_dimensions', 10 );
add_filter( 'the_content', 'remove_thumbnail_dimensions', 10 );
function remove_thumbnail_dimensions( $html ) {
    $html = preg_replace( '/(width|height)=\"\d*\"\s/', "", $html );
    return $html;
}

The filters are as follows:

  • post_thumbnail_html – Filter any post thumbnails
  • image_send_to_editor – Filter the HTML when adding an image to the editor
  • the_content – Will remove all attributes from attached images

If you are a Genesis user, the you have to add one more line along with above code.

// Genesis framework only
add_filter( 'genesis_get_image', 'remove_thumbnail_dimensions', 10 );

One thought on “WordPress Hack: Remove Width And Height Attributes From Images”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.