Tagged: block editor, Lazy Load
-
AuthorPosts
-
January 24, 2022 at 8:15 pm #1336940
Hi guys!
Happy 2022!
Quick question: I want to Lazy Load all images with the exception of the first image on a Post or Page as it affects LCP on Core Web Vitals.
I know I can manage this IF I use the Advanced Layout Editor and insert your Image shortcode. BUT here is what I am using and where I would like to know how to do this:
Block Editor on a post of Page. I look at the image HTML and I do not see the “lazy” as it does it by default.
I did see your Theme Featured Image Block. Does using it instead of the Block editor’s Image block remove the “lazy”?
Thank you in advance!!
Havi
January 25, 2022 at 9:37 am #1337004Hey havi,
Thank you for the inquiry.
Where can we check the post? The image will only be lazy loaded if the loading attribute is set to lazy.
I did see your Theme Featured Image Block.
The theme doesn’t generate any block elements out of the box. Would you mind providing a screenshot?
Best regards,
IsmaelJanuary 25, 2022 at 9:08 pm #1337173Hi Ismael!
Thank you for the reply.
My goal is to stop the 1st image that renders above the fold from being Lazy loaded. The rest of the images on any page/post, should be lazy loaded.
So, my Enfold settings under Performance are set to Enable Lazy Loading.
How do I disable Lazy Loading on the first image that displays on the page? The Page is built with the block editor and not the ALB (which makes it harder to control if it should be Lazy Loaded or not) But I can use this workaround for those Posts that render an image above the fold as well.
Here’s the page: https://staging-mindcrowd.kinsta.cloud/faq/
Is there a CSS class I can create and add to the Image Block Advanced settings?
Please, let me know if you need admin access or if this was maybe fixed on the new version of Enfold that came out today?
Havi
Re: The Featured Image Block is displayed under Blocks > Theme > I see Query Loop, Post Title, Post Content, Post Date, Post Excerpt, Post Featured Image, Login/out and Posts List (as Enfold is the only theme I have installed, I assumed these were Enfold’s – but never mind, This is not important as what I tried didn’t work as Adding a Featured image makes it display at the top of the page and there is no way to hide it from the page)
January 26, 2022 at 5:50 am #1337211Hi,
Thank you for the update.
The loading attribute is probably added automatically because WordPress enables lazy loading by default. If you want to disable this feature, you can add this filter in the functions.php file.add_filter( 'wp_lazy_loading_enabled', '__return_false' );
You can also try and enable the Code Editor in the Options panel and remove the loading attribute manually. Please check the screenshot in the private field.
UPDATE: Looks like the lazy loading option in the theme also toggles the default lazy loading feature by using the same filter above, so you don’t have to add it. Just toggle the lazy loading option in the Performance panel. Let us know if you are able to edit the image block manually.
Best regards,
IsmaelJanuary 26, 2022 at 11:32 pm #1337373Hi Ismael,
I do not want to disable the Lazy Loading for all the images. I never wanted to disable the feature. I want to disable it only for certain images when they render above the fold as this impacts Core Web Vitals (LCP) badly. I did see another ticket where you guys told someone how to lazy load a single image. I want to do the opposite. Not lazy load a single image.
I have already tried using Edit as HTML. This is the code inside of the Block Editor page. As you can see, there is no way to remove the loading attribute manually as it’s added by Enfold. See code below.
<div class="wp-block-image"><figure class="aligncenter size-full"><img src="https://websitenamedotcom/wp-content/uploads/2021/12/mindcrowd-contributors-faq.jpg" alt="MindCrowd Contributors: University of Arizona, TGen, City of Hope National Medical Center, University of Miami Miller School of Medicine, Johns Hopkins School of Medicine, Georgia Institute of Technology, Emory University, Alzheimer's Prevention Initiative." /></figure></div>
This is the HTML that executes on that page for that image – the image attribute loading=”lazy” does not display inside of the Block Editor:
<figure class="aligncenter size-full"><img loading="lazy" width="900" height="180" src="https://webdotcom/wp-content/uploads/2021/12/mindcrowd-contributors-faq.jpg" alt="MindCrowd Contributors: University of Arizona, TGen, City of Hope National Medical Center, University of Miami Miller School of Medicine, Johns Hopkins School of Medicine, Georgia Institute of Technology, Emory University, Alzheimer's Prevention Initiative." class="wp-image-1049" title="MindCrowd " srcset="https://webdotcom/wp-content/uploads/2021/12/mindcrowd-contributors-faq.jpg 900w, https://webdotcom/wp-content/uploads/2021/12/mindcrowd-contributors-faq-300x60.jpg 300w, https://webdotcom/wp-content/uploads/2021/12/mindcrowd-contributors-faq-768x154.jpg 768w, https://webdotcom/wp-content/uploads/2021/12/mindcrowd-contributors-faq-705x141.jpg 705w" sizes="(max-width: 900px) 100vw, 900px"></figure>
There has to be a way to do this. I can do it if I use the ALB because you guys have it as an option on the Advanced tab. But this page and the posts are not created on ALB – they are created with the Block Editor.
I hope there is a solution.
Best regards,
Havi
NOTE: I copied the code I saw when inspecting the page on Chrome into an HTML Block. Changed the loading=”lazy” to loading=”eager” and it worked. Unfortunately that means adding the image, inspecting the code, copying the HTML, adding a new block, pasting, removing the image block, and hopefully you don’t need to edit this image in the future.
So, I tried adding loading=”eager” to the image block in HTML. Did not work. Can we create a new Class with the CSS loading=”eager” that can be applied to the Block under Advanced > Additional CSS Class(es)? How would that work? I added the page where I run this test in the Private content section.
- This reply was modified 2 years, 10 months ago by havi. Reason: Added test results
January 27, 2022 at 10:31 am #1337463Hi,
Thank you for the info.
As you can see, there is no way to remove the loading attribute manually as it’s added by Enfold.
We don’t really think that is the case. We created a new page on our installation, added an Image block but we are not getting the loading attribute even when the lazy loading option is enabled in the theme options. Please check the screenshot in the private field.
Did you check your plugins? There might be a lazy loading option in one of your plugins that adds the loading attribute to the Image block. You can disable the Lazy Loading option temporarily and see if the Image block attribute changes.
Best regards,
IsmaelJanuary 30, 2022 at 1:12 am #1337873Yes, it disappears when I disable Enfold’s option.
<figure class="aligncenter size-full"><img width="900" height="180" src="https://mindcrowd/wp-content/uploads/2021/12/mindcrowd-contributors-faq.jpg" alt="MindCrowd Contributors: University of Arizona, TGen, City of Hope National Medical Center, University of Miami Miller School of Medicine, Johns Hopkins School of Medicine, Georgia Institute of Technology, Emory University, Alzheimer's Prevention Initiative." class="wp-image-1049" title="MindCrowd " srcset="https://mindcrowd/wp-content/uploads/2021/12/mindcrowd-contributors-faq.jpg 900w, https://mindcrowd/wp-content/uploads/2021/12/mindcrowd-contributors-faq-300x60.jpg 300w, https://mindcrowd/wp-content/uploads/2021/12/mindcrowd-contributors-faq-768x154.jpg 768w, https://mindcrowd/wp-content/uploads/2021/12/mindcrowd-contributors-faq-705x141.jpg 705w" sizes="(max-width: 900px) 100vw, 900px"></figure>
I am using my workaround on the same page on staging (and on the live site).
So on the staging page you can see two images. The one on top has been manually inserted via HTML (cannot edit the block editor – it throws an error) and the one on the bottom is the regular image added with the block editor on the page: https://staging-mindcrowd.kinsta.cloud/faq/
I disabled Lazy Loading on Enfold Performance settings and you can see, loading lazy does not show on the code.
Look forward to the solution. This is not a good workaround.
Best regards,
Havi
January 31, 2022 at 4:44 am #1337964Hi,
On our own installation, the loading attribute does not get applied to the Image block regardless of the lazy loading option in the theme, so the attribute is coming from somewhere else. You can check our screenshot above.
Did you install an image compression plugin? Can we access the dashboard?
Best regards,
IsmaelJanuary 31, 2022 at 6:06 pm #1338070Ismael,
Allow me a few days to grant you access as I have been working on staging and I need to make sure I make a note of all the changes so they make it to the live site after client approval is granted.
Meanwhile, could you please share the details of your installation? Because on all the ones I have turning Enfold’s LazyLoading on or off has the same effect. All my installs are on Enfold version 4.8.8.1 installed and WP 5.8.3, Maybe WP changed this on 5.9 (we all know they have been implementing lazy loading by default) or you guys changed it and is now editable in the block editor? Maybe this is a matter of updating. (That would make things go faster – I can remove the workaround after update)
This site has the following plugins (none of them affect images directly – they use them but do not affect them):
Akismet Anti-Spam
Easy Social Share Buttons for WordPress
Simple 301 Redirects
Yoast SEO
Featured Images in RSS for Mailchimp & MoreTIA!
Havi
Hopefully this is simpler than expected.January 31, 2022 at 6:43 pm #1338073Ismael,
This is a wordpress/theme topic.
Check this out: https://make.wordpress.org/core/2020/07/14/lazy-loading-images-in-5-5/#highlighter_892127
Copying certain interesting paragraphs:
By default, WordPress will add loading=”lazy” to all img tags that have width and height attributes present. Technically this is handled on page output, similar to how responsive images are facilitated in WordPress by adding srcset and sizes attributes. To improve server-side performance of the two features, a new wp_filter_content_tags() function has been introduced so that img tags only need to be parsed once while then deferring the modifications to more specific functions related to the feature.By default, WordPress will add a loading=”lazy” attribute to the following images:
images within post content (the_content)
images within post excerpts (the_excerpt)
images within text widgets (widget_text_content)
avatar images (get_avatar)
template images using wp_get_attachment_image() (wp_get_attachment_image)
Developers can customize this behavior through various filters, the most foundational one being wp_lazy_loading_enabled, which receives the following parameters:$default: The boolean default of true to filter.
$tag_name: An HTML tag name. While per current WordPress behavior this will always be img, it should be noted that the loading attribute is a generic attribute and may be expanded to support further elements, e.g. iframes, in the future.
$context: A context string as additional parameters, indicating where the image technically comes from, usually a WordPress hook name. Based on how WordPress itself uses lazy-loading, the context can be one of the five values in parentheses in the list above.And it gives you what the Enfold switch does. Which is great. But then it goes on:
In order to modify the loading attribute for very specific images, there are two different approaches, depending on the type of images:
For images that appear within a content blob (e.g. the_content, the_excerpt, widget_text_content), another new filter wp_img_tag_add_loading_attr can be used, which receives the following parameters:
$value: The loading attribute value, either “lazy” (default), “eager”, or false. If you want to disable lazy-loading for an image, it is strongly recommended to specify false so that the attribute is omitted altogether.
$image: The entire image HTML tag with its attributes.
$context: The context, similar as described for the other filter above.
For example, if you would like to disable lazy-loading for a specific attachment image with ID 42 in size “large” within post content, you could use the following code snippet:And it gives you the snippet – but doing this is similar to my workaround for a specific image (taking the HTML that is the output and changing lazy to eager)
Theme developers are recommended to granularly handle loading attributes for images anytime they rely on wp_get_attachment_image() or another function based on it (such as the_post_thumbnail() or get_custom_logo()), depending on where they are used within templates. For example, if an image is placed within the header.php template and is very likely to be in the initial viewport, it is advisable to skip the loading attribute for that image.
And the Core Web Vitals issue was actually identified by them but it seems that on their experiment they didn’t consider all the 3rd party calls and javascript running times that can delay the LCP image from being loaded, so they write:
“Experiments using Chrome for Android have shown that the impact of such loading=”lazy” images in the initial viewport on the Largest Contentful Paint metric is fairly small, with a regression of <1% at the 75th and 99th percentiles compared to non lazy-loaded images – yet it is a consideration to make where theme developers can apply some fine tuning for even better user experience.”So, maybe this could be a good new feature for Enfold? Allowing to Disable Lazy Loading on certain images from inside the Block Editor?
What concerns me is that you don’t see this on your installation. As it seems to solve mine with my HTML insertion/loading eager workaround. And Enfold is amazing when it comes to the ALB as it allows you to Enable/Disable Loading Lazy on an single image basis. All that’s missing is applying the same to a native or non-ALB image.
Please, let me know if my understanding of what is going on is correct. And if so, maybe this can help others who would like to implement my workaround or the one on functions.php that wordpress offers.
Thank you so much!
Havi
February 1, 2022 at 6:18 am #1338151Hi,
Do you see the same behavior when you update WordPress to the latest version? We are currently using the latest version of WordPress, which is version 5.9 and the latest version of the theme. In this installation, the loading attribute is not automatically added to the Image block regardless of the lazy loading option in the theme. And yes, you can use the filter wp_lazy_loading_enabled to completely disable the lazy loading option as we have suggested previously.
Screenshot: https://1drv.ms/u/s!AjjTfXSRbKTvwB3j46JJ1oaUPI3-
Best regards,
IsmaelFebruary 1, 2022 at 8:40 pm #1338310Hi Ismael,
I never wanted to disable the Lazy Loading on the whole site. (I don’t know why you seem to think so)
Maybe the difference between your environment and mine is the reason for this difference. It will take me a few days to update these sites. As soon as I get to it, I will check and get back to you.
Hopefully, there will be a way to prevent a single image from Loading Lazy. This is what I wanted from day one.
Quick question: I want to Lazy Load all images with the exception of the first image on a Post or Page as it affects LCP on Core Web Vitals.
My goal is to stop the 1st image that renders above the fold from being Lazy loaded. The rest of the images on any page/post, should be lazy loaded.
Anyway, maybe there is a way once I update and you see it and you think I see it too. If there isn’t. I still need a way to do this.
Thank you!
Havi
February 2, 2022 at 6:48 am #1338386Hi,
I never wanted to disable the Lazy Loading on the whole site. (I don’t know why you seem to think so)
Yes, we understand that but since we cannot reproduce the issue on our end, it would help if we could test if disabling the default lazy loading option from WordPress will also disable the loading attribute in your Image block. If it doesn’t, then there is something else that is adding that attribute. Using a code block to add the image manually seems to be sufficient enough in this case.
Let us know once the installation is updated to version 5.9.
Best regards,
IsmaelFebruary 16, 2022 at 5:37 am #1340830Hi Ismael,
Answering your question:
Do you see the same behavior when you update WordPress to the latest version?
Yes.
I see the same behavior. Both wordpress and Enfold are fully updated on other sites and the behavior is identical.
1- If Lazy Loading is enabled on Enfold Performance settings >> loading=”lazy” displays on the HTML for the image
2- If Lazy Loading is disabled on Enfold Performance settings >> there is no loading=”lazy” displaying on the HTML for the imageI can use my workaround with the Block Editor for an image Block. I cannot use my workaround with the Classic Editor on a Blog Post image.
I can use Enfold’s ALB feature if I use the ALB Editor to lazy load or not an image. Maybe this could be a feature to consider in the near future to help with Core Web Vitals? ;) Adding the same Enable Lazy Loading to the image itself but on the Classic and Block Editors? Maybe it’s not feasible but maybe it is.
It would be great. Also, maybe other people have come up with a solution to disable Lazy Loading of a single image (above the fold) on WordPress. But it’s definitely enabled/disabled from Enfold (it is possible that it’s a WordPress feature that Enfold manages. Actually, I’m sure that’s the case. It would be great if it would manage it a wee bit more and that would be awesome.)
Any help or ideas or hope on this matter would be greatly appreciated.
I have no clue why you don’t see it though.
TIA!!
Havi
February 18, 2022 at 4:45 am #1341255Hi,
Have you tried using this filter to disable lazy loading for a specific image?
function avf_skip_loading_lazy_image_42_large( $value, $image, $context ) { if ( 'the_content' === $context ) { $image_url = wp_get_attachment_image_url( 42, 'large' ); if ( false !== strpos( $image, ' src="' . $image_url . '"' ) { return false; } } return $value; } add_filter( 'wp_img_tag_add_loading_attr', 'avf_skip_loading_lazy_image_42_large', 10, 3 );
This will disable the lazy loading attribute for the image with the ID 42.
Best regards,
Ismael -
AuthorPosts
- You must be logged in to reply to this topic.