Skip to content

Instantly share code, notes, and snippets.

View huanyichuang's full-sized avatar

Huanyi Chuang (Eric) huanyichuang

View GitHub Profile
@huanyichuang
huanyichuang / lazyload.php
Last active May 11, 2020 02:25
Prepare WordPress post for Image LazyLoading by replacing the src Attribute
<?php
/**
* Lozad
* Attribute replacement refernce from: https://gist.github.com/ahmadassaf/9332846
*/
function faster_replace_img_src( $content ){
if ( function_exists( 'is_amp_endpoint' ) && ! is_amp_endpoint() ) {
$html = preg_replace_callback( '#(<img\s[^>]*src)="([^"]+)"#' , 'callback_img' , $content );
$html = preg_replace( '/(<img\s[^>]*)class="(.*)"/', '$1 class="lozad $2"' , $html );
$html = preg_replace( '/(<img\s[^>]*)srcset="(.*)"/', '$1 data-srcset="$2"' , $html );