Working with Responsive Images in WordPress Core | @thetorquemag
“All The Word That's Fit To Press.”
A WP Engine
Publication
Feature

Working With Responsive Images In WordPress Core

Responsive design has been around for quite many years by now. And with the advent of WordPress 4.4 in December last year, responsive images became a part of WordPress core.

What does this imply for WordPress users and developers? In this article, we will be discussing what is meant by responsive images in WordPress core, and why you should be happy about it.

Working With Responsive Images In WordPress Core

Background

While responsive images have only recently found their way in WordPress core, the concept of responsive images in itself is not new to the world of WordPress. Prior to version 4.4, this feature was being offered through the RICG Responsive Images plugin, and it is this very plugin that has been integrated in WordPress core in version 4.4.

ricg-responsive

Mode Of Operation

Basically, the feature works by adding two new attributes to all your uploaded images: srcset and sizes. The entire process happens in the background, and when a given image appears on the page, it carries the srcset and sizes attributes, which inform the web browser what version of the image it should use on the basis of the user’s device resolution.

Naturally, there is no visible user interface for this, and the end user does not need to specify any additional information when uploading the images to WordPress. Everything happens in the background, and the end result is that users get crisp and responsive images that look great on their devices. Furthermore, it also yields benefits in terms of performance, as the browser need not download larger images and this can give speed boost to your website.

Theme Developers

As a theme developer, you’ll probably wish to adjust the image attributes in the functions.php file so that your theme can make full use of responsive images. WordPress 4.4 has provided some new functions and many new hooks that you can make use of for this task, and you can find all the info you need in the documentation.

A good example is the Twenty Sixteen theme, which is the new default theme for WordPress. If you check out the theme’s functions.php file, you will notice two extra functions at the end, that are filtering image attributes so that responsive images can fit the breakpoints inside the theme easily. See this:

twenty-16-1

And for thumbnails:

twenty-16-2

The above two functions are filtering the sizes attribute for images in order to make them fit the viewpoints. Of course, this is a sample implementation, and you can edit your own functions accordingly.

Disabling Responsive Images

Considering the fact that responsive images are still a fairly recent addition to WordPress core, there might be issues with certain themes or plugins, or you might face conflicts in terms of code.

If this is the case, you might wish to temporarily disable responsive images. Unfortunately, there is no direct way to do this, so you will probably have to edit the theme’s functions.php file or make use of an external plugin.

You can place the following code in your theme’s functions.php file in order to disable responsive images:

The credit for the above code goes to Stack Overflow user Birgir Erlendsson.

Alternatively, you can also use the Disable Responsive Images plugin which follows a similar approach but does not ask you to edit the theme files.

disable-responsive

Conclusion

While you might need to disable responsive images temporarily if you have issues with your existing set of themes or plugins, you should know that responsive images are the future of the internet. As the web moves more and more towards mobile devices such as smartphones or tablets, responsive web design in general and responsive images in particular become all the more important.

As such, the presence of responsive images is a very welcome addition to WordPress core. This will give a much-needed performance boost in terms of reduction of resource usage and unnecessary requests between the browser and the server, and also yield a faster and better experience for website visitors.

Lastly, since everything happens behind the scenes, there is no need for end users to be worried about configuration or tweaking their WordPress sites.

If you build themes for WordPress, have you given responsive images a spin yet? Share your views in the comments below.

Sufyan bin Uzayr writes for various magazine and blogs, and has authored several books. He blogs about technology, Linux and open source, mobile, web design and development, typography, and Content Management Systems at Code Carbon. You can learn more about him, follow him on Twitter or friend him on Facebook and Google+.

More WordPress News From Torque:

There is one comment

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

TOP