wordpress压缩前端网页代码

Saturday, Nov 28, 2020 | 1 minute read | Updated at Saturday, Nov 28, 2020

@

把下面的代码添加到主题functions.php中

即可压缩网页,删除多余的空格,使网页更加紧凑,

减速网页传输。。。

//前端压缩代码开始

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("	", " ", $buffer[$i]));
                $buffer[$i]=(str_replace("

“, " “, $buffer[$i])); $buffer[$i]=(str_replace(” “, “”, $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.=”

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

//前端压缩代码结束

© 2018 - 2026 vpslala

🌱 Powered by Hugo with theme Dream.