Site logo

[Resolved] Avoid native lazy-loading of featured image

Home Forums Support [Resolved] Avoid native lazy-loading of featured image

Home Forums Support Avoid native lazy-loading of featured image

Viewing 15 posts - 1 through 15 (of 28 total)
  • Author
    Posts
  • #2180033
    Jim

    I do use Autoptimize for lazy-loading, but I find even when it is turned off, the featured image gets the attribute loading="lazy" and is lazy-loaded (Autoptimize instead uses the class “lazyloaded”). An example is https://forestpathology.org/general/wood-decay/

    I’m not sure who/what is adding the attribute loading="lazy" to the featured images. Can you suggest a way to avoid that?

    #2180051
    David
    Staff
    Customer Support

    Hi there,

    it comes from WP – they add the attribute for browsers that support lazy loading natively. See this topic on how to disable it:

    https://generatepress.com/forums/topic/lazy-load-feratured-image-with-w3-and-autoptimize/#post-2129406

    #2180086
    Jim

    Thank you. I think that would turn off the native lazy loading entirely. Since I learned about native lazy loading, I was hoping to drop Autoptimize (it’s the only thing I use it for), and selectively disable native lazy loading for featured images, which have the class ‘wp-post-image’.

    I found the following snippet, and modified by substituting the class, but it doesn’t seem to work for some reason. This code is a bit beyond me.

    
    /**
     * Disable native lazy loading on featured images (class 'wp-post-image').
     * @param mixed $value 
     * @param mixed $image 
     * @param mixed $context 
     * @return mixed 
     */
    function remove_lazy_loading_by_class( $value, $image, $context ) {
        if ( 'the_content' === $context ) {
             if ( false !== strpos( $image, 'wp-post-image' ) ) {
                  return false;
             }
        }
        return $value;
    }
    add_filter( 'wp_img_tag_add_loading_attr', 'remove_lazy_loading_by_class', 10, 3 );
    
    #2180120
    David
    Staff
    Customer Support

    Try this snippet:

    add_filter( 'wp_lazy_loading_enabled', 'db_disable_wp_lazy_first_post_in_loop', 10, 3);
    
    function db_disable_wp_lazy_first_post_in_loop( $default, $tag_name, $context) {
        if ( 'img' === $tag_name && 'wp_get_attachment_image' === $context ) {
            return false;
        }
        return $default;
    }
    #2180135
    Jim

    That’s not working but don’t know why. Some images have the loading=”lazy” attribute and some don’t. Maybe it’s seeing the logo as the “first_post” instead of the featured image?

    #2180160
    Ying
    Staff
    Customer Support

    Hi Jim,

    Logo image is not included in the code.

    I just tested the code, it stops lazy loading the featured image of the first post in archive/blog well.

    #2180178
    Jim

    Hmm, I’m not sure what “first post” refers to. I’m doing this on pages, not blog archives. I want to do it on every page.

    #2180191
    David
    Staff
    Customer Support

    Can you share a link to a page to where it should be applied but is not ?

    #2180332
    Jim

    Yes, the first post of this topic had a link: https://forestpathology.org/general/wood-decay/

    I have deactivated caching, so you should get a live page, and the suggested snippet is active.

    Thanks

    #2180498
    Elvin
    Staff
    Customer Support

    Hi Jim,

    Let’s try a few things:

    For the sake of testing if the lazy load attribute is actually coming from native function, can you try adding this PHP snippet and check?

    add_filter( 'wp_lazy_loading_enabled', '__return_false' );
    Reference: https://developer.wordpress.org/reference/hooks/wp_lazy_loading_enabled/

    This filter completely removes the lazy loading WordPress applies. We then check if the image is affected by this filter.

    If this snippet didn’t remove the lazy loading on ALL the images, there’s a good chance something else is adding it.

    If this is the case, then we start debugging further by disabling all the plugins and reviewing all the PHP snippets added to the site so find out where the lazy loading comes from.

    Can you give it a shot? Let us know how it goes.

    #2180974
    Jim

    Excellent suggestion, Elvin, but I’m afraid the results are difficult to interpret, at least for me. I used Troubleshooting Mode to disable all plugins and switch among themes.

    I looked for the loading="lazy" attribute in the featured image and in 3-4 other images. My functions.php and the snippet you suggested that supposedly kills lazy loading are only active in the GP child theme.

    ***** Presence of loading="lazy" attribute ******
    Theme —- Feat. Image —- Other images
    2020 ———- no ——————- yes
    2022 ———- yes —————— yes
    GP ————- yes —————— some, not all
    GP child —— yes —————— no

    I guess the theme must be adding it, at least for featured image? I’m a bit confused.

    #2181011
    David
    Staff
    Customer Support

    Do you have this code added ?

    add_filter( 'wp_lazy_loading_enabled', '__return_false' );

    If not – can you add it, then clear any caches and let me know

    #2181224
    Jim

    Yes, as mentioned, that code is active in GP child theme only and gave the results above.

    In GP child, without the code, all images have loading="lazy".

    With the code, only the featured image has loading="lazy".

    Again, all these results are with all plugins disabled.

    My functions.php is not putting the attribute in the featured image, because as you see above, it is also there in the pristine GP parent theme.

    #2181278
    David
    Staff
    Customer Support

    the Theme doesn’t add the lazy attribute, it may be a case the filter needs firing later – try this to see if it removes more of them :

    add_filter( 'wp_lazy_loading_enabled', '__return_false', 999, 3 );

    #2181321
    Jim

    Still the same result with the altered add_filter(). Could it be other arguments need to be supplied, $tag or $context?

Viewing 15 posts - 1 through 15 (of 28 total)
  • You must be logged in to reply to this topic.