3

i want to remove lazy loading on the first image in Wordpress, for every posts, pages and home.

I don't use any plugins for lazy loading image, i need a solution for the default lazy loading add by wordpress.

I found this script for functions.php, but it is for WP Rocket plugin, and i haven't this plugin.

So i modified it in this:

function add_responsive_class($content){
    if ( is_single() || is_page() || is_front_page() || is_home() ) {
        $content = mb_convert_encoding($content, 'HTML-ENTITIES', "UTF-8");
        $document = new DOMDocument();
        libxml_use_internal_errors(true);
        $document->loadHTML(utf8_decode($content));

        $imgs = $document->getElementsByTagName('img');
        $img = $imgs[0];

        if ($imgs[0] == 1) { // Check if the post has images first
            $img->setAttribute('class','aligncenter size-full remove-lazy');
            $html = $document->saveHTML();
            return $html;
        }
        
        else {
            return $content;
        }
     }
     else {
         return $content;
     }
}
add_filter ('the_content', 'add_responsive_class');

Now, i need to remove the lazy load only on image with class "aligncenter size-full remove-lazy".

I try with this, after the above code, but i have an error and it doesn't work:

function remove_lazy_loading_for_specific_class( $attributes ) {
    $attributes[] = 'class="aligncenter size-full remove-lazy"';
    return $attributes;
}
add_filter( 'wp_img_tag_add_loading_attr', 'remove_lazy_loading_for_specific_class' );

How can I do that? Thanks

2 Answers 2

0

Not sure if this would be helpful but instead of all of this why not give this image an ID or a class and remove the lazy attribute that WordPress adds by default using the removeAttribute() method.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi Sohila Khaled, thanks but i need to remove lazy for all first image of every posts on the site. So i need a function, because manually it would be impossible, there are so many posts :)
no, the code in that page is valid for the plugin "Lazy Loader". I don't use any plugins for lazy loading image, i need a solution for the default lazy loading add by wordpress :( thanks anyway
0

I solved only with this :)

function add_responsive_class($content){
    if ( is_single() || is_page() || is_front_page() || is_home() ) {
        $content = mb_convert_encoding($content, 'HTML-ENTITIES', "UTF-8");
        $document = new DOMDocument();
        libxml_use_internal_errors(true);
        $document->loadHTML(utf8_decode($content));

        $imgs = $document->getElementsByTagName('img');
        $img = $imgs[0];

        if ($imgs[0] == 1) { // Check if the post has images first
            $img->removeAttribute( 'loading' );
            $html = $document->saveHTML();
            return $html;
        }
        
        else {
            return $content;
        }
     }
     else {
         return $content;
     }
}
add_filter ('the_content', 'add_responsive_class');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.