I have an image that I would like to adjust the loading attribute for.
<img loading="lazy" class="no-lazy" src="apple.jpg" alt="apple">
The following code changes the loading attribute of images with class=”no-lazy” from lazy to eager:
add_filter( 'wp_img_tag_add_loading_attr', 'skip_lazy_load', 10, 3 );
function skip_lazy_load( $value, $image, $context ) {
if ( strpos( $image, 'no-lazy' ) !== false ) $value = 'eager';
return $value;
}
The issue in WordPress is that the class is added to the figure and not the image as follows:
<figure class="no-lazy"><img loading="lazy" src="apple.jpg" alt="apple"></figure>
How do I use preg_match to achieve the same thing?
'/<figure [^£]*?my_css_class[^£]*?</figure>/'
PS – I am not familiar at all with preg_match and that wp_img_tag_add_loading_attr has been deprecated.