Lazy loading allows websites with a lot of content (especially images, audio, and iframes) to load much faster. Generally, it’s a good practice to lazy load as much content as possible. Sometimes, however, lazy loading isn’t such a good idea – especially when it comes into conflict with analytics tracking.
Here’s some quick technical background:
src (source) attribute that tells your computer where to find the resource it’s supposed to display inside the frame.However, lazy loading sometimes changes the src attributes of iframes, especially when it’s being done by popular WordPress optimization plugins like Smush Pro. As a consequence, the iframe API isn’t able to track those lazy-loaded videos.
Usually, the solution is pretty simple – most optimization plugins will allow some amount of control over what gets lazy loaded and what doesn’t, although the granularity of such control varies. WP Rocket (our preferred optimization plugin) allows users to specify URLs and paths that should never be lazy-loaded, which is extremely convenient. On the other hand, Smush Pro allows users to specify what kinds of content are lazy-loaded, and doesn’t have a particular option for YouTube videos (only iframes in general). Depending on how many iframes are on a page and how much content they need to load in turn, refusing to lazy load iframes can hurt site speed.
Assuming for the moment that it’s a pill we’d rather not swallow, let’s look at our other options.
After poking around in the Smush Pro code, I found that it uses WordPress’s native embed methods to lazy load iframes, but that it won’t lazy load any iframes that have a class attribute of “no-lazyload”. Well, perfect – sounds like it’s just a matter of adding that class in the page editor. Not quite…
The client uses WPBakery as their page editor, and although there are options to add custom classes and IDs, these only apply to the container element WPBakery creates. The actual iframe element is uneditable this way, so we’re going to need to get creative.
Remember WordPress’s native embed methods? Lucky for us, WordPress lets us hook into these and write our own code (with blackjack and filter hooks). The most obvious hook to present itself was wp_embed_handler_youtube – sounds like exactly what we want, right? Well…
Strangely, wp_embed_handler_youtube only works on YouTube videos that weren’t embedded using oEmbed (which, to avoid a longish explanation of oEmbed, is how most YouTube videos are embedded). Nifty filter, huh?
Luckily, we’re not out of options yet. The filter embed_oembed_html lets us directly edit the HTML content of any embed WordPress is responsible for, so we can use that to add the no-lazyload class to our iframe and finally call it a day.
In the end, I’m happy enough with this solution, though I wish it was a bit more elegant. embed_oembed_html allows the (raw) HTML content to be edited as a string, which is not nearly as nice as the structured objects used by, say, wp_embed_handler_youtube. The finished code does the following:
class attribute begins and ends using strpos().class attribute exists (based on whether or not the variables created in step 2 have values), add the no-lazyload class to it.class attribute doesn’t exist, insert one with no-lazyload as its value.This filter can be added to the functions.php file. Ultimately, I’d recommend using this with discretion – it may be less important to track YouTube videos than it is to optimize the site speed. It depends on how much value you put on tracking YouTube videos and what your threshold for “good site speed” is. This can, of course, be fine-tuned further: ideally, you could write some additional code to set up a custom options page wherein a list of YouTube video URLs could be specified, and the filter would check against that list to see what particular videos shouldn’t be lazy-loaded. However, depending on your client and your timetable, this might be more trouble than it’s worth.
Developed (did not implement) a script to avoid lazy loading YouTube videos.
add_filter( 'embed_oembed_html', 'dont_lazyload_yt' );
function dont_lazyload_yt( $code ){
//Only don't lazyload YouTube videos.
if (strpos($code, 'youtube.com') === false) {
return $code;
}
// Find the position of the class attribute in the iframe element
$startPos = strpos($code, 'class="');
$endPos = strpos($code, '"', $startPos + 7);
// Check if class attribute exists
if ($startPos !== false && $endPos !== false) {
// If class attribute exists, add "no-lazyload" to the classlist
$class = substr($code, $startPos + 7, $endPos - $startPos - 7);
$newClass = $class ? $class . ' no-lazyload' : 'no-lazyload';
$html = substr_replace($code, $newClass, $startPos + 7, $endPos - $startPos - 7);
} else {
// If class attribute doesn't exist, add the class attribute with value "no-lazyload"
$code = str_replace('<iframe ', '<iframe class="no-lazyload" ', $code);
}
return $code;
}