Cases

Lazy loading: Setup Manual for WordPress

05 August, 2020

The website load speed is one of the most important website ranking factors. While more customers are attracted to the website, it should load faster. This can be done with browser image lazy loading. Our Plerdy team has prepared a detailed manual with 4 different methods of easy implementation of Lazy loading of your website.

What is “Lazy loading”?

Lazy loading (aka browser image lazy loading) is one of the methods of accelerating the website load. It loads the photo when the user scrolled the page to the part with images.

Please note! loading Googlebot attribute supports Chrome 76 only. As a rule, now all users have Chrome 83 or Chrome 84.

When “lazy loading” should be implemented?

Browser image lazy loading is required in the following cases:

  • The speed of loading your website is more than 5-7 sec and all recommendations from the speed test services are followed.
  • The competitors’ website is much faster than yours.
  • You use many images on the website/blog pages.
  • Your website is hosted on a weak server.
  • 50-60% of users visit your resource from smartphones.
  • Your website has been rated lower than 50 points by PageSpeed ​​Insights.

Ready-made solutions on configuring Lazy loading

On the Internet, many different recommendations on implementing and configuring Lazy loading can be found. In most cases, you need programmers’ support that can implement lazy loading with several functions. There are other solutions (e.g., if you have a WordPress website, you can use ready-made plugins). But there is one catch. When installing a plugin its functions and styles are added to the website code. So the website may load slower, but we need to accelerate the website load. So there is a contradiction. That is why we prepared 3 ready-made solutions used for our website and we can say for sure that this is working.

Setting up Lazy loading – method #1

One of the ways to set up Lazy loading is to add the function to the “functions.php” theme. It will add 'loading=”lazy”' attribute to all 'img loading="lazy"' and 'iframe loading="lazy" iframe' tags automatically when a new post or page is created.

It is implemented in the following way:
add_filter( 'save_post', 'add_lazy_load', 10, 3 );
function add_lazy_load($post_id, $post, $update)
{
if (wp_is_post_revision($post_id)) {
return;
}
if ( ! class_exists( 'DOMDocument', false ) )
return;
remove_action('save_post', 'add_lazy_load');
$post_status = get_post_status();
if ($post_status != 'draft') {
$document = new DOMDocument();
$document->loadHTML(mb_convert_encoding($post->post_content, 'HTML-ENTITIES', 'UTF-8'));
$images = $document-getElementsByTagName('img');
foreach ($images as $image) {
$image->getAttribute('load');
if (!$image->getAttribute('loading') || $image->getAttribute('loading') != 'lazy') {
$image->setAttribute('loading', 'lazy');
}
}
$iframes = $document->getElementsByTagName('iframe');
foreach ($iframes as $iframe) {
$iframe->getAttribute('load');
if (!$iframe->getAttribute('loading') || $iframe->getAttribute('loading') != 'lazy') {
$iframe->setAttribute('loading', 'lazy');
}
}
$html = $document-saveHTML();
wp_update_post(array(
"ID" = $post_id,
"post_content" => html_entity_decode($html),
));
}
add_action('save_post', 'add_lazy_load', 10, 3);
}

This method is suitable for new pages only. The second method of setting up Lazy loading is applied for created pages and posts.

Setting up Lazy loading – method #2

This method of setting up Lazy loading is similar to the first method, but here the database is updated for saved posts only.

Add 'loading="lazy"' attribute to all 'img' and 'iframe' tags in the post table in the database. If the ‘loading’ attribute is not used for the tags any more, this can be made by a simple replacement:

UPDATE `wp_posts` t1, `wp_posts` t2
SET t1.`post_content`= REPLACE(t2.`post_content`, 'img ', 'img loading="lazy" ')
WHERE t1.id = t2.id

UPDATE `wp_posts` t1, `wp_posts` t2
SET t1.`post_content`= REPLACE(t2.`post_content`, 'iframe ', 'iframe loading="lazy" ')
WHERE t1.id = t2.id

If the ‘loading’ attribute has been added to the 'img loading="lazy"' and 'iframe loading="lazy"' tags in the database already, use queries with regexes for replacing.

This can be checked using the following query:
SELECT * FROM `wp_posts` WHERE `post_content` REGEXP 'img[A-Za-z'"= ]+loading'
SELECT * FROM `wp_posts` WHERE `post_content` REGEXP 'iframe[A-Za-z'"= ]+loading'

The first and second methods belong to database operations. The following examples demonstrate how to set up Lazy loading using the coding in the themes.

Setting up Lazy loading – method #3

If the 'the_post_thumbnail()' function is called in the themes of your website, you can reproduce the 'loading' = 'lazy' attribute with a parameter using this function.

It is implemented in the following way:
the_post_thumbnail('full', array('loading' = 'lazy'));

The drawback of this method is that this 'the_post_thumbnail()' function may be absent from your website.

Setting up Lazy loading – method #4

The fourth method is adding the 'loading' = 'lazy' attribute to all 'img loading="lazy"' and 'iframe loading="lazy"' tags in all website themes.

It is implemented in the following way:
img src="https://test.com/wp-content/speed.svg" loading="lazy" data-src="https://test.com/wp-content/speed.svg"

“Lazy loading” and problem during setting up

Sometimes errors occur when implementing Lazy loading. As a rule, this leads to closing the media content for crawlers. So the users would not see the images on your website in the search results.

For making sure that the Lazy loading attribute has been implemented and set up properly, you may use the following methods:

  • In the image, search enter the image alt and try finding it among other results.
  • Use the following script. Using these commands you can run the following script:

git checkout https://github.com/GoogleChromeLabs/puppeteer-examples
cd puppeteer-examples
npm i
node lazyimages_without_scroll_events.js -h

Conclusions

If you haven’t still implemented the browser image lazy loading, don’t hesitate! This allows improving the page load rate and website usability.