- This topic has 10 replies, 4 voices, and was last updated 3 years, 5 months ago by
David.
-
AuthorPosts
-
November 20, 2022 at 9:11 am #2424309
Thorsten
Hej,
I have created an element as block hook for generate_before_content and add there a dynamic image. This image is the featured image of the post this block hook will added to.
So Googles page speed complains this image is part of the largest contentful paint area and is lazy loaded. I have searched this forum and tried to add the skip-lazy class to it. Without any change.
And deactivating lazy loading globally in WordPsress and added again with Autoptimized sound a little bit crazy to me, because I don’t need Autooptimize at all.Do you have a solution for this?
Thanks in advance
ThorstenNovember 20, 2022 at 1:58 pm #2424523Ying
StaffCustomer SupportHi Thorsten,
Can you try adding an additional CSS class to the image block, eg.
featured-image, then add the below snippet:add_filter( 'render_block', function( $block_content, $block ) { if ( ! empty( $block['attrs']['className'] ) && 'featured-image' === $block['attrs']['className'] ){ $block_content = str_replace( 'lazy', 'eager', $block_content ); } return $block_content; }, 10, 2 );November 20, 2022 at 3:09 pm #2424574Thorsten
No, I am sorry, that doesn’t work. There is always lodaing=”lazy”.
November 20, 2022 at 5:43 pm #2424659Fernando Customer Support
Hi Thorsten,
For reference, can you provide the link to the site in question?
You may use the Private Information field for this: https://docs.generatepress.com/article/using-the-premium-support-forum/#private-information
November 21, 2022 at 1:16 am #2424933Thorsten
Yes, of course. And the page isn’t a great secret, only work in progress.
But you can see it here: Sample page with lazy loaded featured imageThe image in the green area is the featured image (in block hook added as dynamic image)
November 21, 2022 at 1:58 am #2424989Fernando Customer Support
I see. Since you have GenerateBlocks, can you try using a GenerateBlocks Image Block instead for that image, and then add
skip-lazyto the class list of this Image Block?Adding Custom Classes: https://wordpress.com/support/wordpress-editor/adding-additional-css-classes-to-blocks/
November 21, 2022 at 4:48 am #2425199Thorsten
I have changed the block hook, but it is always the same 🙁
November 21, 2022 at 5:32 am #2425279David
StaffCustomer SupportHi there,
WP provides the
wp_get_attachment_image_attributesfilter hook to set attributes on an image.
And as you have added theskip-lazyclass we can use that in that filter to remove the loading attribute.Try this PHP Snippet:
add_filter( 'wp_get_attachment_image_attributes', 'fp_no_lazy_featured_image', 10, 3 ); function fp_no_lazy_featured_image( $attr ) { if ( false !== strpos( $attr['class'], 'skip-lazy' ) ) { unset( $attr['loading'] ); } return $attr; }November 21, 2022 at 11:03 am #2426013Thorsten
No change 🙁
November 21, 2022 at 11:08 am #2426028Thorsten
But I have fixed your code snippet:
Instead
unset( $attr['loading'] );I am using
$attr['loading'] = "eager";And this finally works.
November 22, 2022 at 5:47 am #2427081David
StaffCustomer SupportGlad to hear you got it working.
-
AuthorPosts
- You must be logged in to reply to this topic.