How to change “src” to “data-src” and “srcset” to “data-srcset” for images?

I’m using lazy load script in my theme and it’s required “data-src” and “data-srcset” in images. In my code I can do it on my own but I can’t do that for images directly from editor. I was trying use this:

function example_lazy_load($html, $id, $caption, $title, $align, $url) {
$src = ‘src=”‘.get_template_directory_uri().’/i/thumb.png”‘;
$html = str_replace( “<img src”, “<img {$src} data-src”, $html );
return $html;
}
add_filter( ‘image_send_to_editor’, ‘example_lazy_load’, 10, 9 );

but without any results. I don’t want do that via jQuery.

I am using lozad script – https://github.com/ApoorvSaxena/lozad.js?files=1

Read more here:: How to change “src” to “data-src” and “srcset” to “data-srcset” for images?

Leave a Reply

Your email address will not be published. Required fields are marked *