- This topic has 27 replies, 5 voices, and was last updated 3 years, 5 months ago by
David.
-
AuthorPosts
-
April 6, 2022 at 8:12 am #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?April 6, 2022 at 8:27 am #2180051David
StaffCustomer SupportHi there,
it comes from WP – they add the attribute for browsers that support lazy loading natively. See this topic on how to disable it:
April 6, 2022 at 8:53 am #2180086Jim
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 );
April 6, 2022 at 9:23 am #2180120David
StaffCustomer SupportTry 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; }
April 6, 2022 at 9:37 am #2180135Jim
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?
April 6, 2022 at 9:56 am #2180160Ying
StaffCustomer SupportHi 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.
April 6, 2022 at 10:17 am #2180178Jim
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.
April 6, 2022 at 10:41 am #2180191David
StaffCustomer SupportCan you share a link to a page to where it should be applied but is not ?
April 6, 2022 at 1:45 pm #2180332Jim
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
April 6, 2022 at 8:26 pm #2180498Elvin
StaffCustomer SupportHi 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.
April 7, 2022 at 7:10 am #2180974Jim
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 —————— noI guess the theme must be adding it, at least for featured image? I’m a bit confused.
April 7, 2022 at 7:47 am #2181011David
StaffCustomer SupportDo 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
April 7, 2022 at 8:16 am #2181224Jim
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.
April 7, 2022 at 9:13 am #2181278David
StaffCustomer Supportthe 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 );
April 7, 2022 at 10:06 am #2181321Jim
Still the same result with the altered add_filter(). Could it be other arguments need to be supplied, $tag or $context?
-
AuthorPosts
- You must be logged in to reply to this topic.