Skip to content

Instantly share code, notes, and snippets.

@thagxt
Created January 27, 2016 14:22
Show Gist options
  • Save thagxt/2cb75cc62b2b9b9319b3 to your computer and use it in GitHub Desktop.
Save thagxt/2cb75cc62b2b9b9319b3 to your computer and use it in GitHub Desktop.
Function to add a class to all your "POST" images in WordPress and get them ready for lazyload.
<?php
function lazyload_filter_content($content) {
global $post; // getting The whole post objects
$content = $post->post_content;
if( is_singular() && is_main_query() ) {
//Renaming images for lazy loader!!!
// Replace all image tags with the appropriately formatted HTML
// We add a data-unveil attribute so we can target a CSS transition (fade-in)
$content = preg_replace( '#<img([^>]+?)src=[\'"]?([^\'"\s>]+)[\'"]?([^>]*)>#',
sprintf( '<img${1}src="%s" data-src="${2}"${3} class="unveil"><noscript><img${1}src="${2}"${3}></noscript>', $placeholder ), $content );}
return $content; // returning the edited $content
}
add_filter('the_content', 'lazyload_filter_content');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment