Skip to content

Instantly share code, notes, and snippets.

@Antoinebr
Last active May 19, 2020 15:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Antoinebr/6e38db662cffa2473109499da5f879c9 to your computer and use it in GitHub Desktop.
Save Antoinebr/6e38db662cffa2473109499da5f879c9 to your computer and use it in GitHub Desktop.
Hook to use lazysize lib
// The lazyoad class is added
<?php the_post_thumbnail('medium', array('class' => "img-responsive lazyload")); ?>
<?php
//
// If LazyLoad class is present on the postthubmnail we replace the html
// markup to fit the lazysizes html markup plugin requirements
//
add_filter('post_thumbnail_html','lazyloading_thumb',10,5);
function lazyloading_thumb ( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
if (strpos($attr['class'], 'lazyload') !== false):
$html = str_replace("", 'data-sizes="auto"', $html);
$html = str_replace("sizes", "data-sizes", $html);
$html = str_replace("srcset", "data-srcset", $html);
$html = str_replace("src", "data-src", $html);
endif;
return $html;
}
<?php
add_action( 'wp_enqueue_scripts', 'register_my_scripts' );
function register_my_scripts() {
// get le librairie and save it locally https://github.com/aFarkas/lazysizes/blob/gh-pages/lazysizes.min.js
wp_enqueue_script( 'lazysizes', get_template_directory_uri() . '/js/libs/lazysizes.min.js',false, '3.0', true );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment