WordPress前端html代码压缩优化方法

WordPress前端html代码压缩优化方式

为了优化站点的访问速率,对网页进行压缩长短常不错的一条方式。压缩页面缩减了页面的体积提拔了访问速率。

安装功能代码

//压缩html代码 
function wp_compress_html(){
    function wp_compress_html_main ($buffer){
        $initial=strlen($buffer);
        $buffer=explode("<!--wp-compress-html-->", $buffer);
        $count=count ($buffer);
        for ($i = 0; $i <= $count; $i++){
            if (stristr($buffer[$i], '<!--wp-compress-html no compression-->')) {
                $buffer[$i]=(str_replace("<!--wp-compress-html no compression-->", " ", $buffer[$i]));
            } else {
                $buffer[$i]=(str_replace("\t", " ", $buffer[$i]));
                $buffer[$i]=(str_replace("\n\n", "\n", $buffer[$i]));
                $buffer[$i]=(str_replace("\n", "", $buffer[$i]));
                $buffer[$i]=(str_replace("\r", "", $buffer[$i]));
                while (stristr($buffer[$i], '  ')) {
                    $buffer[$i]=(str_replace("  ", " ", $buffer[$i]));
                }
            }
            $buffer_out.=$buffer[$i];
        }
        $final=strlen($buffer_out);   
        $savings=($initial-$final)/$initial*100;   
        $savings=round($savings, 2);   
        $buffer_out.="\n<!--压缩前的巨细: $initial bytes; 压缩后的巨细: $final bytes; 节省:$savings% -->";   
    return $buffer_out;
}
ob_start("wp_compress_html_main");
}
add_action('get_header', 'wp_compress_html');

安装方式:将以上代码粘贴到WordPress正题目录下的functions.php文件的最后一个 ?> 之前便可。

修正方式:

将上述代码中的最后三行:

ob_start("wp_compress_html_main");
}
add_action('get_header', 'wp_compress_html');

修正为:

if ( !is_admin() ) { 
        ob_start("wp_compress_html_main");
    }
}
add_action('init', 'wp_compress_html');

目前此代码在知更鸟正题Begin正题测试成功

插件方式

后台搜索WP-HTML-Compression便可,不提议用,太多插件会拖慢WP执行速率

扫描二维码关注公众号,回复: 3600916 查看本文章

希望以上的文章对各位有用,如果觉得不错给我顶一下吧!更多和WordPress前端html代码压缩优化方法相关的问题或者对德国虚拟空间哪个稳定有疑惑也欢迎大家咨询。

猜你喜欢

转载自www.cnblogs.com/fubitech/p/9805300.html