It replace everything except header & footer. I've tried different priority for this function but it doesn't seem to work.
I think the_content
is only for content and not header/footer, but after reading documentation, I still cannot find what else to use.
function add_lazyload($content) {
$content = mb_convert_encoding($content, 'HTML-ENTITIES', "UTF-8");
$dom = new DOMDocument();
@$dom->loadHTML($content);
foreach ($dom->getElementsByTagName('img') as $node) {
$oldsrc = $node->getAttribute('src');
$node->setAttribute("data-src", $oldsrc );
$newsrc = null;
$node->setAttribute("src", $newsrc);
}
$newHtml = preg_replace('/^<!DOCTYPE.+?>/', '', str_replace( array('<html>', '</html>', '<body>', '</body>'), array('', '', '', ''), $dom->saveHTML()));
return $newHtml;
}
add_filter('the_content', 'add_lazyload');
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745344648a4623486.html
评论列表(0条)