Remove width and height image attributes with jQuery

When adding images into a WordPress post, the system automatically adds its height and width attributes to the picture. That’s usually a good thing, but there are times when you won’t like the fact that every image gets those attributes.

If you want to strip the attributes right at the point of upload, you can do that through a simple function which we showed you last time.

But if you read the article or tried the function yourself, you’ve noticed that it only affects images uploaded after the function was installed into your WP system. If you already have a database full of posts with inserted images, the aforementioned function won’t help you much with your old image attributes.

Since you probably can’t go through each and every image manually and delete width and height one by one, you are probably in a need of another function which will strip those attributes for all existing images embedded in your posts. In that case, you’re in the right spot ‘cause we’re about to show you a simple way of automatically removing width and height image attributes with few lines of jQuery code. Don’t get scared with the previous phrase – you don’t even have to know what jQuery is, just follow the next three steps and you’ll be done in a jiffy.

Remove width and height image attributes:

  1. Open header.php file from your theme folder
  2. Copy and paste this code anywhere between <head> and </head> tags:
  3. <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script type="text/javascript">
    jQuery.noConflict();
    jQuery(document).ready(function($){
    $('img').each(function(){
    $(this).removeAttr('width')
    $(this).removeAttr('height');
    });
    });
    </script>
  4. Save changes

And you’re done! The code will do its magic after you open a post which contains an image and will remove width and height tags from it.

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

Leave a Reply

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