Skip to content

Instantly share code, notes, and snippets.

@tux255
Created May 6, 2018 07:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tux255/e94690a1bc03dca40e583c72721b19cd to your computer and use it in GitHub Desktop.
Save tux255/e94690a1bc03dca40e583c72721b19cd to your computer and use it in GitHub Desktop.
Change Wordpress final output. LazyLoad integration for IMG tags. Not finished
function callback($buffer) {
$patterns = [
'/<img(.*) src=/',
'/<img(.*) class="/'
];
$replacements = [
'<img${1} data-src=',
'<img${1} class="b-lazy '
];
$buffer = preg_replace($patterns, $replacements, $buffer);
return $buffer;
}
function buffer_start() {
ob_start("callback");
}
add_action('wp_head', 'buffer_start');
function buffer_end() {
ob_end_flush();
}
add_action('wp_footer', 'buffer_end');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment