最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Use DOMDocument with ob_start breaks my HTML code

programmeradmin1浏览0评论

I'm trying to use a PHP function with DOMDocument for substitute some line in my final HTML code using Wordpress.

The function seems to be fine, but it breaks my HTML code in some points.

i.e. original code:

            document.addEventListener( 'DOMContentLoaded', function() {
                jQuery && jQuery( function( $ ) {
                    // insert placeholder for events injected when a product is added to the cart through AJAX
                    $( document.body ).append( '<div class=\"wc-facebook-pixel-event-placeholder\"></div>' );
                } );
            }, false );

Changed code:

    document.addEventListener( 'DOMContentLoaded', function() {
        jQuery & jQuery( function( $ ) {
            // insert placeholder for events injected when a product is added to the cart through AJAX
            $( document.body ).append( '<div class=\"wc-facebook-pixel-event-placeholder\">' );
        } );
    }, false );

In this case has changed the '&&' with '&' in the JS function. But there are other glitches like this, like space added or removed. It put also some "" tag randomly in the page. This arbitrary are making my page not working fine, as they breaks codes used by my theme.

This is my complete code:

function fix_iubenda_pixelWoocommerce_obStart() { ob_start("fix_iubenda_pixelWoocommerce"); }
function fix_iubenda_pixelWoocommerce_obStop() { ob_end_flush(); }
add_action('wp_head', 'fix_iubenda_pixelWoocommerce_obStart');
add_action('wp_foot', 'fix_iubenda_pixelWoocommerce_obStop');

function fix_iubenda_pixelWoocommerce( $buffer ) {
        $doc = new \DOMDocument;
        $doc->preserveWhiteSpace = 1;
        $doc->strictErrorChecking = 0;
        $doc->formatOutput=0;
        libxml_use_internal_errors(true);

        //$doc->loadHTML($buffer);
        $encoded = mb_convert_encoding( $buffer, 'HTML-ENTITIES', 'UTF-8' );
        $ok = $doc->loadHTML( $encoded, LIBXML_HTML_NODEFDTD | LIBXML_NOBLANKS );
        if ( !$ok ) {
           return $content;
        }
        $xpath = new \DOMXPath($doc);
        $nodeList = $xpath->query('//script[contains(., "fbq(")]');
        foreach ($nodeList as $node) {
                $node->nodeValue = $node->nodeValue ;
                $node->setAttribute('type', 'text/plain');
                $node->setAttribute('class', '_iub_cs_activate-inline');
        }

        $buffer = $doc->saveHTML();
        return $buffer;
}

It seems that's not related to my manipulation, but there are something in $doc->saveHTML() that breaks the HTML code.

Any suggestion on how to change my code in order to make this script works fine is very welcome.

If you have also solution different than ob_start for change my HTML output are also welcome. I need to to a "search & replace" in all the code, including header and footer, not only the content.

Thanks

发布评论

评论列表(0)

  1. 暂无评论