Search code examples
wordpresslazy-loading

Wordpress 5.9 and wp_lazy_loading_enabled filter issue on images


since WP 5.5, I had to add this function to my theme to prevent rendering issues in an image slider due to automatic lazy loading in WP:

    add_filter( 'wp_lazy_loading_enabled', '__return_false' );

This was working quite well since it was removing the automatic parameter "loading=lazy" on images elements.

Yesterday, after upgrading to WP 5.9, I'm facing the same issue again with my slider and I see that the "loading=lazy" parameter is back on all images!

How can I fix that? Thanks


Solution

  • You can try one of these two solutions:

    $attr['loading'] = false;
    return wp_get_attachment_image( $attachment_id, $size, $icon, $attr );
    

    Or:

    add_filter( 'wp_get_attachment_image_attributes', function( $attr, $attachment, $size ) {
        if ( $attachment->post_mime_type === 'image/svg+xml' ) {
            unset( $attr['loading'] );
        }
        return $attr;
    } );
    

    Answer is from here: https://developer.wordpress.org/reference/functions/wp_lazy_loading_enabled/#comment-4226