Images are generally the heaviest assets on a webpage- so if you’re interested in fast load times for users, this is one of the best places to optimize.
Lazy Load Your Images
One popular technique for speeding up the initial load time is called “lazy loading”. It works by replacing the source of images (src=”/example.gif”) with a transparent placeholder image (src=”/pixel.gif”). This allows everything else on the page (text,fonts,scripts,etc) to render first.
The original source for the image is generally saved into a data attribute, such as “data-src”, so it can be accessed in later.
This is helpful because we now have complete control over when images get loaded. At any time the source of the image (src=”/transparent.gif”) can be replaced by the actual source in the data attribute. (See a simple jQuery plugin example).
Several lazy load scripts also work by only loading in the actual images once they enter the viewport, which means the images in the footer won’t load in until the user has actually scrolled down that far. This can save bandwidth for the user as images that don’t get viewed, won’t get loaded.
Also, many jQuery plugins use a quick fadeIn function when replacing images with their actual source, which can be a nice visual effect.
Lazy Loading in WordPress
The key in WordPress is to filter the_content, post_thumbnail_html, and get_avatar content so that any images will have their source replaced by the transparent placeholder.
The best solution I found for this is in the Lazy Load plugin code by Automattic, Jake Goldman, 10Up, and Mohammad Jangda. If you’re simply looking for the easiest way to lazy load images on your site I highly recommend using the plugin.
The Lazy Load plugin uses a single function to filter any $content passed to it, and replace any images within that $content with the placeholder image. Here’s what it looks like (slightly modifed):
function add_image_placeholders( $content ) {
// Don't lazyload for feeds, previews, mobile
if( is_feed() || is_preview() || ( function_exists( 'is_mobile' ) && is_mobile() ) )
return $content;
// Don't lazy-load if the content has already been run through previously
if ( false !== strpos( $content, 'data-src' ) )
return $content;
// In case you want to change the placeholder image
$placeholder_image = apply_filters( 'lazyload_images_placeholder_image', get_template_directory_uri() . '/images/pixel.gif' );
// This is a pretty simple regex, but it works
$content = preg_replace( '#<img([^>]+?)src=[\'"]?([^\'"\s>]+)[\'"]?([^>]*)>#', sprintf( '<img${1}src="%s" data-src="${2}"${3}><noscript><img${1}src="${2}"${3}></noscript>', $placeholder_image ), $content );
return $content;
}
Then, you can add your filters to replace images in specific bits of content.
Post Content:
add_filter( 'the_content', 'add_image_placeholders', 99 );
Featured Images:
add_filter( 'post_thumbnail_html', 'add_image_placeholders', 11 );
Avatars (generally in comments):
add_filter( 'get_avatar', 'add_image_placeholders', 11 );
Javascript
Once you’ve implemented the code above in your theme, you shouldn’t be seeing any images in the content that you filtered- just the placeholder pixel.
So, now you can replace the images when you choose with a bit of jQuery:
jQuery('img').attr('src',jQuery(this).attr('data-src'));
The Lazy Load WordPress plugin uses jQuery Sonar, which doesn’t replace images until it is in the viewport window.
There are also a ton of other scripts that do something similar. I say find one that works for you (it’ll likely need a bit of customization), or roll your own.
Should you lazy load images in a theme?
I originally had a homebrewed version for lazy loading thumbnails in my Visual theme, but have since decided to remove it.
I think functionality like this is much better handled by a plugin like Lazy Load. I plan to start encouraging users to install this plugin, and may even add a notice and automatic loader for it.