Getting WordPress to play nice with responsive images
- Knowledge needed: Basic PHP and CSS
- Requires: WordPress install, text editor of choice
- Project time: 10 minutes
Frontend designer David Smith responds to Jesse Friedman's article on responsive design with WordPress and presents an alternative way to deal with images
If you subscribe to the print version of .net magazine (if not, why not!?), you will have seen that this month’s issue features an excellent article on “Responsive Design with WordPress”.
In the article, author Jesse Friedman outlines a load of really useful techniques for making the most of, and overcoming, inherent WordPress functionality in order to produce a truly effective responsive website. If you’re thinking of producing a responsive site with WordPress you should definitely pick up a copy of his article. It’s a must-read.
Having recently rebuilt my personal blog on WordPress using a responsive, mobile-first approach I was familiar with some of the techniques covered in the article. However, the one item that really stood out for me was Jesse’s approach to enabling fluid images via jQuery.
Advertisement
The problem with WordPress and “fluid images”
As I'm sure you’re all aware 'fluid images' – which make use of max-width: 100% – require that images have no fixed width or height in order that they can scale to the size of their container. Unfortunately, WordPress automatically calculates the dimensions of images generated from the Media library and adds the corresponding width and height attributes to any <img> tags.
This is great for page rendering speed but it throws a massive spanner in the works when it comes to creating responsive layouts as image dimensions are no longer constrained to the size of their container.
That’s a problem.
The non-jQuery solution
In his article Jesse’ suggests using jQuery to remove the width and height attributes from all <img> tags on the page once it has loaded. This certainly works but when building my site I didn’t like the idea of relying on JavaScript to achieve this, especially if there were a lot of images on the page in question.
This is where WordPress filters came to the rescue.
The WordPress codex defines a filter as:
“...hooks that WordPress launches to modify text of various types before adding it to the database or sending it to the browser screen.”
As it turns out this is exactly what we need. By setting up a filter to run on all images as the final action before they are rendered on the page, we can use PHP to remove the width and height attributes thereby circumventing the need for (potentially) expensive DOM manipulation via JavaScript.
The code
- /**
- * RESPONSIVE IMAGE FUNCTIONS
- */
- add_filter( 'post_thumbnail_html', 'remove_thumbnail_dimensions', 10 );
- add_filter( 'image_send_to_editor', 'remove_thumbnail_dimensions', 10 );
- function remove_thumbnail_dimensions( $html ) {
- $html = preg_replace( '/(width|height)=\"\d*\"\s/', "", $html );
- return $html;
- }
In the code above we’re adding two filters using the add_filter function. The first argument is the filter we want to hook into and the second specifies the function we want to run when the filter is called.
In our code we hook into two obscure functions:
- post_thumbnail_html – images retrieved with the post_thumbnail()
- image_send_to_editor – images added to the editor
We then using a regular expression to remove both the width and height attributes from the image tags. Now when our images are sent to the browser they can be fully 'fluid' just like Mr Marcotte told us they should.
A confession
I have to confess having had the idea of using add_filter to remove attributes I could not for the life of me locate the correct WordPress filters to hook into.
After a lot of searching I finally came across this extremely helpful post on Wordpress Stack Exchange by Nathaniel Taintor which provided the information about the two filters I needed.
Caveats
As far as I know the only major draw-back to this approach is that it doesn’t remove the width and height attributes from all images on your site. I found this to be an issue, specifcally with the Gravatar images WordPress uses in comments.
If anyone has a solution to this issue please do leave a comment so we can all benefit.
I hope this has been useful and demonstrated an alternative to relying on JavaScript to implement “fluid images” on WordPress websites.




14 comments
Comment: 1
Someone told be about this plugin a while ago http://wordpress.org/extend/plugins/search-regex/ The only issue I had with it was that my image dimensions varied across the entire site so didn't didn't it that useful.
Comment: 2
It seems to be a great idea in practice but isn't it adding extra php requests to the website server which are then slowing down websites? Adding this to the fact you can't alter gravatars and some other images, isn't the jQuery option most suitable?
Comment: 3
I believe this is what the default theme for WordPress to make its images responsive.
Comment: 4
But this way isn't the same image served but just at smaller dimensions?
I think it's better to deliver smaller size thumbnails for devices with known bandwidth limitation.
Great job anyway!
Comment: 5
@stoffb I guess it's a trade off. I'm not an expert but I can't imagine the technique in the post is a greatly server intensive task as we're not manipulating images, we're just removing attributes. My personal preference is for this to be dealt with on the backend rather than having to wait for jQuery to parse the DOM on page load. Love to hear any research you have on this.
@rkrug That's true but then I believe you'd then be forcing the img to be 100% wide (width: 100%). This would might then cause distortion of the img due to stretching if the width of the container exceeded that of the original image dimensions. Does that make sense? Please correct me if I'm wrong. I also think it's better to just remove them rather than have to overide them...
@GeorgeDina I agree. The post didn't really intend to cover this topic but perhaps I could have mentioned it. Ideally you should start with a small image and enhance to larger images for more able devices. Nonetheless the technique above is still valid if you want your images to be responsive.
Comment: 6
http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src
example:
$image = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large' );
echo '';
Comment: 7
Comment: 8
@kremalicious No need to be sorry :) That's correct. However, the original comment said "...you could set the width/max-width to 100%...". Having width 100% would stretch the image. However, as you correctly point out setting width and height to "auto" would solve the issue. Thanks for pointing out this alternative solution - really appreciate the feedback. Looks like there are a few options to resolving this issue!
Comment: 9
Comment: 10
But please be aware of another problem with this: without width/height on the img the space required for the image isn't reserved on page load. This will make the layout of the page change during initial loading, cause the img elements are collapsed first and won't get their full height on the page before the browser has loaded the complete image file
Comment: 11
Comment: 12
Comment: 13
Any suggestion I can i get the same using the filter?
Comment: 14