在以往的wordpress优化教程中,我给大家推荐了2款代码压缩插件,今天主要是分享插件的代码版本,也就是不使用插件,直接将代码丢在functions.php中就可以使插件生效,好吧,又消灭一个插件!
/* *压缩html代码 * http://www.wpmee.com/wp-compress-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("nn", "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–>
- 不被压缩的部分
- <!–wp-compress-html no compression–><!–wp-compress-html–>