David Stockdale's Scrapcode

Lazy Loading Broke wp-embedded-content

In a previous post I described how after updating Jetpack I had found my embedded posts no longer displayed their featured images.

This was due to Jetpack’s Lazy Image Loading leaving these images unloaded.

Instead from what I can gather the blank images displayed where simply placeholders.

Before update
After update

This was quite a problem until I found a relatively simple fix:

Add the image classes used within embedded posts to Jetpacks array of images not to lazy load!

This can be done simply by adding this to your “functions.php” file:

/**
 * Prevents any image with the class "attatchment" from loading.
 * (Fixes Jetpack Lazy Loading leaving embedded post images forever unloaded).
 */
add_filter( 'jetpack_lazy_images_blocked_classes', 'mysite_customize_lazy_images' );
function mysite_customize_lazy_images( $blocked_classes ) {
    $blocked_classes[] = 'attachment';
    return $blocked_classes;
}

Though this most simple version does of course apply to other images within my theme such as archive pages.

That can be fixed simply by customising it a bit and being more specific.

Such as using:

<?php echo wp_get_attachment_image( $thumbnail_id, $image_size, "", ["class" => "embeddy"] ); ?>

Rather than:

<?php echo wp_get_attachment_image( $thumbnail_id, $image_size); ?>

Within “embed-content.php” and then simply using “embeddy” rather than “attatchment” as the blocked class.

Leave a Reply