0

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');
  • There is no equivalent filter for the header and footer (or the sidebar, for that matter). It appears that you're trying to change the uploads URL everywhere, but the uploads URL shouldn't be hardcoded anywhere in the header or footer. Could you please elaborate on what problem you're actually trying to solve. – Jacob Peattie Jul 5 '19 at 1:02
  • My goal is actually for lazy loading. You can click the "edited" link to see my actual function. Because the function longer and harder to understand, I'm using simpler example above. – Isak Pontus Jul 5 '19 at 2:22
  • looks like you'd probably need to output buffer the entire page from start to finish to even have a chance of doing what you're trying to achieve. – majick Jul 5 '19 at 8:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.