=== Lazy Load Fix for Divi ===
Contributors: jerrystewart99
Tags: Lazy, Image, Lazy Load, above the fold, Divi
Requires at least: 5.7.0
Tested up to: 7.0
Requires PHP: 7.0
Stable tag: 1.1.2
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html


Provides precise control of which media (images) are lazy-loaded for optimum page load performance.


== Description ==

Offers Fine-grained control for manually setting the threshold which images on a Divi page are lazy-loaded.

Includes a fix for a Divi bug that causes incorrect calculation of the lazy-load threshold.


= Background =

To improve page load performance, modern browsers allow for images and other media to be lazy loaded. Lazy loading defers media loading until after the first page render.

For best page load performance, only *Below The Fold (btf)* media should be lazy loaded. *Below The Fold* means the part of the page that is not visible on first page load, prior to any scrolling.

Google Page Speed Insights (PSI) will rightly complain if media *atf* are lazy loaded, and also if media *btf* are not lazy loaded.

In version 5.7.0, Wordpress added a feature that automatically adds the loading="lazy" attribute to media it considers to be *btf*. What it considers *btf* basically means everything after the 3rd media component irrespective of page design.

Divi's Critical CSS feature attempts to do a better job of determining the fold by analysing the raw page modules' shortcodes. 
Divi offers different threshold settings of where it considers *The Fold* to be. eg. 'Low', 'Medium' or 'High'. (This setting is found in Divi Settings|Performance|Critical CSS Threshold.)

The exact position of *The Fold* however, depends on a few factors, perhaps the most obvious is the screen width. Divi's analysis is approximate and is easily confused by the use of additional custom css.


= How the Lazy-Load-Fix-for-Divi plugin works =

This plugin allows the developer to determine the exact position of *The Fold* by manually setting it's location for each page. You can do this after making a simple observation in a browser at the screen widths you want to optimise for.

For example, suppose you decide that you want to optimise for the following screen resolutions:

`1200 x 834` - desktop
`320 x 820` - mobile

(these are the approximate resolutions that Google PSI uses)

Then, after completing your page design, inspect your page in a browser that is set to these dimensions`(note 1)` and identify the first Divi module that is *btf*.

To manually set the threshold, add one or more of the custom classes to the first Divi module *btf*.`(note 2)`. 

Allowed values are:

`ww-btf-desktop`
`ww-btf-mobile`
`ww-btf-tablet` (see note 3)

All media items *btf* will be lazy-loaded, those *atf* won't be.

Repeat in the same way to set up thresholds for each page you want to optimise for.

* Note that `ww-btf-tablet` is not implemented in this version and will be treated as `ww-btf-mobile`

= Notes =

**Note 1 -- Set a browser to a specific resolution** I suggest using Chrome with Developer Tools enabled (right click, select *Inspect*). Resizing the window with a mouse shows the window size next to the cursor.
**Note 2 -- Add a custom class to a Divi module** Edit the module and select the *Advanced* tab and use *CSS ID & Classes*
**Note 3 --** Tablet devices are not implemented in this plugin version and will be treated as if they were `ww-btf-mobile`


== Divi BUG ==

In addition, this plugin fixes a code bug in Divi relating to the lazy loading of images. (introduced in 4.5.2 and still present in 4.27.6)

The problem occurs in 

`Divi\includes\builder\core.php @ line #63`
- the remove_filter() call quietly fails because the priority is not specified. This bug results in the Wordpress media counting logic to be run twice which defeats the lazy-load threshold when using Divi. 
The Wordpress function remove_filter() specifies parameter 3 as `int $priority = 10`. If the priority is not 10 (in this case it is actually 12), the remove_filter call fails.

I guess Elegant Themes's priority are now with version 5 so this bug may not ever get fixed. If the bug ever gets fixed, this bugfix part of this plugin will have no effect.

This bugfix is distinct from and will work independently from the custom setting of the custom classes described above.


**Note:** I contacted Elegant Themes about the bug and was advised to use the plugin: ["Disable Lazy Loading"](https://wordpress.org/plugins/disable-lazy-loading/) 
which does an excellent job of disabling **ALL** lazy loading. 
In this case though, this is not the behaviour I want.

My motivation for writing this plugin is to improve the Google PSI score on Mobile and Desktop devices. 

I welcome helpful comments and suggestions :)


This plugin will have no effect if used on Divi < 4.5 or on Wordpress < 5.7.0


== Installation ==

Lazy Load Fix For Divi just as you would any other WP Plugin:

1. [Download Lazy Load For Divi](https://wordpress.org/plugins/lazy-load-fix-for-divi "Lazy Load Fix for Divi") from WordPress.org.

2. Unzip the .zip file.

3. Upload the Plugin folder (lazy-load-fix-for-divi/) to the wp-content/plugins folder.

4. Go to [Plugins Admin Panel](https://codex.wordpress.org/Administration_Panels#Plugins "Plugins Admin Panel") and find the newly uploaded Plugin, "Lazy Load Fix for Divi" in the list.

5. Click Activate Plugin to activate it.



== Frequently Asked Questions ==

= Will this Plugin work on my WordPress.com website? =

Sorry, this plugin is available for use only on self-hosted (WordPress.org) websites.

= Can I use this plugin with the Divi Builder =

This plugin has only been tested with the Divi Theme to date. It may work with Divi Builder using a different theme. Please do let me know how that goes :)

= What Divi settings should I use =

This is a performance tweak plugin. It is only effective if Divi's performance options are ENABLED, and in particular Critical CSS **must** be ENABLED for this plugin to have any effect. (This is the Divi default)

= Will this work on Divi 5?

No. Divi 4 only please.

= How do I use this plugin? =
 
Install and activate. Add custom classes as per description. That's it!
 
= How to uninstall the plugin? =
 
Simply deactivate and delete the plugin. It will clean up nicely and leave no trace.


== Screenshots ==

1. Above *The Fold* image loading without plugin - Atf image being lazy loaded => **BAD**
2. Above *The Fold* image loading with plugin enabled - aft image **not** lazy loaded

== Changelog ==

= 1.1.2 =
Tested up to 7.0

= 1.1.1 =
Tested up to 6.9

= 1.1 =
Added ability to set *The Fold* per page, per device

= 1.0 =
Inital Release - Divi bugfix only

== Upgrade Notice ==

= 1.1 =
Added ability to set *The Fold* per page, per device
