How to remove width and height attributes from images in WordPress

Uploading new images into WordPress is relatively easy. If you’re using standard media uploader which comes packed with WordPress, you’ll be able to easily upload and organize images, use those in several sizes and use them as featured images or simply display them in posts and pages.

Because of that, there aren’t as many settings which more advanced users would want from WordPress.

Every time that you upload an image and decide to use it in your posts, the system will automatically add width and height HTML attributes to describe image dimensions. If you are using WordPress as a simple blogging platform or have a professional theme up and running, this won’t mean much to you. But in case you’re building your own theme or modifying one into your personal website, you might want to define image parameters manually.

Instead of actually going through each and every image and striping those attributes by hand, we’re about to show you a snippet which will do that for you.

Remove width and height attributes from images:

  1. Open functions.php
  2. Copy and paste the code:
  3. function remove_img_attributes( $html ) {
    $html = preg_replace( '/(width|height)="\d*"\s/', "", $html );
    return $html;
    }
    add_filter( 'post_thumbnail_html', 'remove_img_attributes', 10 );
    add_filter( 'image_send_to_editor', 'remove_img_attributes', 10 );
    
  4. Save changes

Note that this newly installed function will only take care of the images you upload after the function was saved to your functions.php file. Older images will still have their attributes attached. Instead of manually removing attributes for the older images, you can automatize the process with a simple jQuery function.

If you need more control over images in WordPress, see how to generate additional image sizes or change the way WordPress compresses images.

START YOUR OWN BLOG

This guide is an introduction to mastering the art of blogging. It provides easy to follow steps to start, maintain, and grow your blog.

Read the guide

One thought on “How to remove width and height attributes from images in WordPress

  1. […] Internet matters a lot, and luckily, WordPress already comes with lots of cool stuff to manage and handle your images. But, some of you may not know about these things, if you have just been starting out on WordPress. […]

Leave a Reply

Your email address will not be published. Required fields are marked *