Documentation

Replacing data within outputted HTML using DOMDocument and DOMXPath

Posted in: Getting Started (3) Plugin Options Explained (22) Actions / Filters (19) How To (5)      Html Content (5)    Replace (10)  

Plugin provide multiple options to control outputted HTML data, however giving the complexity of existing WordPress themes and plugins, sometimes certain area may not be covered. In such cases you can extend plugin functionality (see Create a custom Module Component, extending the plugin functionality), or through filters wph/ob_start_callback which would be the easiest way.

HTML data processing through DOMDocument and DOMXPath is a powerful method to control everything on the outputted code, which ensure is also a valid markup. This is a basic example on how to parse the buffer into a DOMDocument and then DOMXPath object


        add_filter( 'wph/ob_start_callback', 'custom_ob_start_callback' ); 
    function custom_ob_start_callback( $buffer )
        {
            //check if PHP DOMDocument instance is available
            if ( ! class_exists( 'DOMDocument', false ) )
                return $buffer;

            $doc = new DOMDocument();
            $doc->preserveWhiteSpace    = true;
                                
            //preserve html document encoding
            if ( @$doc->loadHTML(mb_convert_encoding($buffer, 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD) === false )
                return $buffer;
            
            $doc->encoding              = 'UTF-8';
            $doc->formatOutput          = true;
            
            //use XPath    
            $xpath = new DOMXPath($doc);
           
            //further data manipulation goes here
            //search and iteration area
            
            $doc->normalizeDocument();
            
            $buffer =   $doc->saveHTML( $dom->documentElement );
                
            return $buffer;
               
        }

Presuming we need a class ‘envato’ replaced for all instances within HTML, we can use the following code:


        add_filter( 'wph/ob_start_callback', 'custom_ob_start_callback' ); 
    function custom_ob_start_callback( $buffer )
        {
            //check if PHP DOMDocument instance is available
            if ( ! class_exists( 'DOMDocument', false ) )
                return $buffer;

            $doc = new DOMDocument();
            $doc->preserveWhiteSpace    = true;
                                
            //preserve html document encoding
            if ( @$doc->loadHTML(mb_convert_encoding($buffer, 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD) === false )
                return $buffer;
            
            $doc->encoding              = 'UTF-8';
            $doc->formatOutput          = true;
            
            //use XPath    
            $xpath = new DOMXPath($doc);
            
            //make a search on envato class
            $nodes = $xpath->query('//*[contains(concat(" ", normalize-space(@class), " "), " envato ")]');
            if($nodes->length < 1)
                return $buffer;
            
            //iterate through all founds    
            foreach ($nodes as $node) 
                {
                    $class  =   $node->getAttribute('class');
                    $class  =   str_replace('envato', '', $class);
                    $class  =   trim($class);
                    
                    $node->attributes->getNamedItem("class")->nodeValue = $class;
                }
            
            $doc->normalizeDocument();
            
            $buffer =   $doc->saveHTML( $dom->documentElement );
                
            return $buffer;
               
        }

DOMXPath is a powerful engine to traverse and query HTML similar to the way things are done in JavaScript and CSS. More details can be found at The DOMXPath class

Share on FacebookShare on Google+html-using-domdocument-and-domxpath/"> =ht-"> 3io="row"> =ht =ht_ontrol =ht- Supk" hmain-nav-mobileontr =htol

We (selly chttpabo DOMDoer supk" hlass.do our bMDocto helpis is one. Supk" h -ite?urkes 16 cyleoDOMs://wfra_ /a">ontent" id="

id="kfootro>

=ht_ontr =ht =ht_ DOMDocisp;e" href="http:ontr =ht DOMDoORw0KG =htol =ht- Ght entatio madment/h4> aside-nav-pro"socialnavigsG =ht://www aside-nav-pro"socialnple opt://wwwject-/e/pnmxity of eencodps com/mp-hidas Linuxlass. Wi">o menb>le _r wrtio/sb>lass.URL R wrtio/sb>lle Coms. linux class="limet="_blank" clas wi">o m class="lim css3 class="lim Rw0K5 class="lim worert_em class="lim ro">< as a id="pSpacas a id="p id="pagid="menu-recper">osts-2io="row"> =ht =ht_recper_ iesolma =ht- Recpert/h4>asiul> (10)(10) hide-le Com=" (bitropt wores frod counts can { visual a> hidelle Com/sha rolim ass=" (10)(10)(10)worert_em-xity stio >Sht-up Wproper PROpde> n such casMity Stio/sha rolim ass=" (10)(10)XSSlass.doRF securtiy issuesfilter( AVADA Ts yo/sha rolim ass=" (10)< a id="p d="menu-> 6io="row"> =ht =ht_ontrol =ht- Our/Twithin!t/h4>asin-nav-mobileontr =htol<-nav-mobileoweecs id="pageain-nav-mobile">