The WPFolks app is out on iPhone and Android. Free, no ads. Download on the App Store Get it on Google Play
Skip to main content
Login Join
Snippet · PHP

Automatically Add loading=”lazy” to Images

Shared by Amit Dholiya · June 29, 2026

50 views
1 upvote
Back to Snippets

Improve page speed by automatically adding lazy loading to images in post content.

Steps

  1. Open your WordPress theme functions.php file.
  2. Add the code below at the end of the file.
  3. Save the file.
  4. Clear your cache and test a blog post.

Done — images will load only when they are about to enter the viewport.

function add_lazy_loading_to_images($content) {
    return str_replace('<img', '<img loading="lazy"', $content);
}
add_filter('the_content', 'add_lazy_loading_to_images');
Know a different way to do this? Add your approach as a variation so folks can compare them side by side.
Submit a variation

0 comments

Proudly supported by