• Đăng ký
  • Đăng nhập

Minify Html trong wordpress không cần plugin

📅 — 👀 838 — 👦

Trang này có hữu ích không?

👍 Có (12)        👎 Không (10)


Khi chúng ta view source của 1 website trong trình duyệt thì có đôi khi ta thấy code đó như 1 đám rừng, rất khó đọc. Đó gọi là Minify Html (hay còn gọi là nén source trong đó có css và javascript inline).

Nếu chúng ta Minify html thì website sẽ bỏ nhưng ký tự trắng và xuống dòng dư thừa. Điều này giúp cho website ít tốn dung lượng khi render website (tối ưu website và tốt cho seo) và hạn chế dân developer khó soi code hơn (chỉ tương đối thôi). Sau đây là đoạn code (tạm thời ta lưu thành file kk-compression.php)

File kk-compression.php

class WP_HTML_Compression
{
        // Settings
        protected $compress_css = true;
        protected $compress_js = false;
        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 minifyHTML($html)
        {
                $pattern = '/script).*?|style).*?|--).*?-->|[\/\w.:-]*)(?:".*?"|\'.*?\'|[^\'">]+)*>|(?((compress_js;
                                        
                                } else if ( !empty($token['style'] ) ) {
                                    
                                        $strip = $this->compress_css;
                                        
                                } else if ( $content == '' ) {
                                    
                                        $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', '', $content);                                                
                                        }
                                }
                                
                        } else {
                            
                                if ( $tag == 'pre' || $tag == 'textarea' || $tag == 'script' ) {
                                    
                                        $raw_tag = $tag;
                                        
                                } else if ( $tag == '/pre' || $tag == '/textarea' || $tag == '/script' ) {
                                    
                                        $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++(?', '/>', $content);                                                
                                        }
                                        
                                }
                                
                        }
                        
                        if ( $strip ) {
                            
                                $content = $this->removeWhiteSpace($content);                                
                        }
                        
                        $html .= $content;                        
                }
                
                return $html;
        }
                
        public function parseHTML($html)
        {
                $this->html = $this->minifyHTML($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' );

Trả lời


📁 Wordpress
🔖 , ,