• 1.
    // Minify theme
  • 2.
    class WP_HTML_Compression
  • 3.
    {
  • 4.
        // Settings
  • 5.
        protected $compress_css = true;
  • 6.
        protected $compress_js = true;
  • 7.
        protected $info_comment = true;
  • 8.
        protected $remove_comments = true;
  • 9.
     
  • 10.
        // Variables
  • 11.
        protected $html;
  • 12.
        public function __construct($html)
  • 13.
        {
  • 14.
            if (!empty($html))
  • 15.
            {
  • 16.
                $this->parseHTML($html);
  • 17.
            }
  • 18.
        }
  • 19.
        public function __toString()
  • 20.
        {
  • 21.
            return $this->html;
  • 22.
        }
  • 23.
        protected function bottomComment($raw, $compressed)
  • 24.
        {
  • 25.
            $raw = strlen($raw);
  • 26.
            $compressed = strlen($compressed);
  • 27.
            
  • 28.
            $savings = ($raw-$compressed) / $raw * 100;
  • 29.
            
  • 30.
            $savings = round($savings, 2);
  • 31.
            
  • 32.
            return '<!--HTML compressed, size saved '.$savings.'%. From '.$raw.' bytes, now '.$compressed.' bytes-->';
  • 33.
        }
  • 34.
        protected function minifyHTML($html)
  • 35.
        {
  • 36.
            $pattern = '/<(?<script>script).*?<\/script\s*>|<(?<style>style).*?<\/style\s*>|<!(?<comment>--).*?-->|<(?<tag>[\/\w.:-]*)(?:".*?"|\'.*?\'|[^\'">]+)*>|(?<text>((<[^!\/\w.:-])?[^<]*)+)|/si';
  • 37.
            preg_match_all($pattern, $html, $matches, PREG_SET_ORDER);
  • 38.
            $overriding = false;
  • 39.
            $raw_tag = false;
  • 40.
            // Variable reused for output
  • 41.
            $html = '';
  • 42.
            foreach ($matches as $token)
  • 43.
            {
  • 44.
                $tag = (isset($token['tag'])) ? strtolower($token['tag']) : null;
  • 45.
                
  • 46.
                $content = $token[0];
  • 47.
                
  • 48.
                if (is_null($tag))
  • 49.
                {
  • 50.
                    if ( !empty($token['script']) )
  • 51.
                    {
  • 52.
                        $strip = $this->compress_js;
  • 53.
                    }
  • 54.
                    else if ( !empty($token['style']) )
  • 55.
                    {
  • 56.
                        $strip = $this->compress_css;
  • 57.
                    }
  • 58.
                    else if ($content == '<!--wp-html-compression no compression-->')
  • 59.
                    {
  • 60.
                        $overriding = !$overriding;
  • 61.
                        
  • 62.
                        // Don't print the comment
  • 63.
                        continue;
  • 64.
                    }
  • 65.
                    else if ($this->remove_comments)
  • 66.
                    {
  • 67.
                        if (!$overriding && $raw_tag != 'textarea')
  • 68.
                        {
  • 69.
                            // Remove any HTML comments, except MSIE conditional comments
  • 70.
                            $content = preg_replace('/<!--(?!\s*(?:\[if [^\]]+]|<!|>))(?:(?!-->).)*-->/s', '', $content);
  • 71.
                        }
  • 72.
                    }
  • 73.
                }
  • 74.
                else
  • 75.
                {
  • 76.
                    if ($tag == 'pre' || $tag == 'textarea')
  • 77.
                    {
  • 78.
                        $raw_tag = $tag;
  • 79.
                    }
  • 80.
                    else if ($tag == '/pre' || $tag == '/textarea')
  • 81.
                    {
  • 82.
                        $raw_tag = false;
  • 83.
                    }
  • 84.
                    else
  • 85.
                    {
  • 86.
                        if ($raw_tag || $overriding)
  • 87.
                        {
  • 88.
                            $strip = false;
  • 89.
                        }
  • 90.
                        else
  • 91.
                        {
  • 92.
                            $strip = true;
  • 93.
                            
  • 94.
                            // Remove any empty attributes, except:
  • 95.
                            // action, alt, content, src
  • 96.
                            $content = preg_replace('/(\s+)(\w++(?<!\baction|\balt|\bcontent|\bsrc)="")/', '$1', $content);
  • 97.
                            
  • 98.
                            // Remove any space before the end of self-closing XHTML tags
  • 99.
                            // JavaScript excluded
  • 100.
                            $content = str_replace(' />', '/>', $content);
  • 101.
                        }
  • 102.
                    }
  • 103.
                }
  • 104.
                
  • 105.
                if ($strip)
  • 106.
                {
  • 107.
                    $content = $this->removeWhiteSpace($content);
  • 108.
                }
  • 109.
                
  • 110.
                $html .= $content;
  • 111.
            }
  • 112.
            
  • 113.
            return $html;
  • 114.
        }
  • 115.
            
  • 116.
        public function parseHTML($html)
  • 117.
        {
  • 118.
            $this->html = $this->minifyHTML($html);
  • 119.
            
  • 120.
            if ($this->info_comment)
  • 121.
            {
  • 122.
                $this->html .= "\n" . $this->bottomComment($html, $this->html);
  • 123.
            }
  • 124.
        }
  • 125.
        
  • 126.
        protected function removeWhiteSpace($str)
  • 127.
        {
  • 128.
            $str = str_replace("\t", ' ', $str);
  • 129.
            $str = str_replace("\n",  '', $str);
  • 130.
            $str = str_replace("\r",  '', $str);
  • 131.
            
  • 132.
            while (stristr($str, '  '))
  • 133.
            {
  • 134.
                $str = str_replace('  ', ' ', $str);
  • 135.
            }
  • 136.
            
  • 137.
            return $str;
  • 138.
        }
  • 139.
    }
  • 140.
     
  • 141.
    function wp_html_compression_finish($html)
  • 142.
    {
  • 143.
        return new WP_HTML_Compression($html);
  • 144.
    }
  • 145.
     
  • 146.
    function wp_html_compression_start()
  • 147.
    {
  • 148.
        ob_start('wp_html_compression_finish');
  • 149.
    }
  • 150.
    add_action('get_header', 'wp_html_compression_start');
  • 151.
     
// Minify theme class WP_HTML_Compression { // Settings protected $compress_css = true; protected $compress_js = true; protected $info_comment = true; protected $remove_comments = true; // Variables protected $html; public function __construct($html) { if (!empty($html)) { $this->parseHTML($html); } } public function __toString() { return $this->html; } protected function bottomComment($raw, $compressed) { $raw = strlen($raw); $compressed = strlen($compressed); $savings = ($raw-$compressed) / $raw * 100; $savings = round($savings, 2); return '<!--HTML compressed, size saved '.$savings.'%. From '.$raw.' bytes, now '.$compressed.' bytes-->'; } protected function minifyHTML($html) { $pattern = '/<(?<script>script).*?<\/script\s*>|<(?<style>style).*?<\/style\s*>|<!(?<comment>--).*?-->|<(?<tag>[\/\w.:-]*)(?:".*?"|\'.*?\'|[^\'">]+)*>|(?<text>((<[^!\/\w.:-])?[^<]*)+)|/si'; preg_match_all($pattern, $html, $matches, PREG_SET_ORDER); $overriding = false; $raw_tag = false; // Variable reused for output $html = ''; foreach ($matches as $token) { $tag = (isset($token['tag'])) ? strtolower($token['tag']) : null; $content = $token[0]; if (is_null($tag)) { if ( !empty($token['script']) ) { $strip = $this->compress_js; } else if ( !empty($token['style']) ) { $strip = $this->compress_css; } else if ($content == '<!--wp-html-compression no compression-->') { $overriding = !$overriding; // Don't print the comment continue; } else if ($this->remove_comments) { if (!$overriding && $raw_tag != 'textarea') { // Remove any HTML comments, except MSIE conditional comments $content = preg_replace('/<!--(?!\s*(?:\[if [^\]]+]|<!|>))(?:(?!-->).)*-->/s', '', $content); } } } else { if ($tag == 'pre' || $tag == 'textarea') { $raw_tag = $tag; } else if ($tag == '/pre' || $tag == '/textarea') { $raw_tag = false; } else { if ($raw_tag || $overriding) { $strip = false; } else { $strip = true; // Remove any empty attributes, except: // action, alt, content, src $content = preg_replace('/(\s+)(\w++(?<!\baction|\balt|\bcontent|\bsrc)="")/', '$1', $content); // Remove any space before the end of self-closing XHTML tags // JavaScript excluded $content = str_replace(' />', '/>', $content); } } } if ($strip) { $content = $this->removeWhiteSpace($content); } $html .= $content; } return $html; } public function parseHTML($html) { $this->html = $this->minifyHTML($html); if ($this->info_comment) { $this->html .= "\n" . $this->bottomComment($html, $this->html); } } protected function removeWhiteSpace($str) { $str = str_replace("\t", ' ', $str); $str = str_replace("\n", '', $str); $str = str_replace("\r", '', $str); while (stristr($str, ' ')) { $str = str_replace(' ', ' ', $str); } return $str; } } function wp_html_compression_finish($html) { return new WP_HTML_Compression($html); } function wp_html_compression_start() { ob_start('wp_html_compression_finish'); } add_action('get_header', 'wp_html_compression_start');

Copy Code to Clipboard Raw

Sign-in or Create a Free Account.

Snippi uses Twitter or Facebook to register and create accounts. Registration will enable you to keep track of your snippets. We never spam your timeline or wall. Ever.

Sign-in with Facebook Sign-in with Twitter