WordPress 修改记录之网页代码压缩
为了让wordpress可以更快的打开,一直在不停的折腾中,虽然效果不是很明显,但一定是有的,蚊子肉也是肉啊,快一点是一点啊,不是很复杂只要在模板目录下的 functions.php 文件中添加以下代码 ,如果想要在网页源文件末尾查看压缩信息,请删除文中的注释。
//压缩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');
<!--wp-compress-html--><!--wp-compress-html no compression--> 此处代码不会被压缩,主要是避免压缩带来的错误,比如JS错误 <!--wp-compress-html no compression--><!--wp-compress-html-->
空空如也!