Created
November 18, 2023 18:25
-
-
Save sabrysuleiman/93da70af5cf6726d0ab4a819f0513a25 to your computer and use it in GitHub Desktop.
lazy load wordpress content images
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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