Skip to content

Instantly share code, notes, and snippets.

@sabrysuleiman
Created November 18, 2023 18:25
Show Gist options
  • Save sabrysuleiman/93da70af5cf6726d0ab4a819f0513a25 to your computer and use it in GitHub Desktop.
Save sabrysuleiman/93da70af5cf6726d0ab4a819f0513a25 to your computer and use it in GitHub Desktop.
lazy load wordpress content images
function add_lazy_loading_to_images($content) {
// Add loading="lazy" to all <img> tags in the content
$content = preg_replace('/<img(.*?)src=(["\'])(.*?)\2(.*?)>/i', '<img$1loading="lazy" src=$2$3$2$4>', $content);
return $content;
}
add_filter('the_content', 'add_lazy_loading_to_images');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment