像哥这样的屌丝站长娃子,没钱去搞起流弊配置的服务器或者vps,就只好在程序上下手啦.
删除所有前端页面内的空格,是一个不错的节约页面字节大小的办法.
之前哥一直使用Autoptimize这个腻害的插件,它不仅能够压缩html,还能把css与js合并.但是哥的小囧客圈因为使用了cos-html-cache生成html,每次更新缓存都需要重新生成页面.
这样下来,就仅仅用到压缩html的功能了,如果是这样,那不如使用下面的可爱的代码版:
代码添加至 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 no compression--> <p> 在注释的保护下 WP HTML Compression 将不会压缩此段代码</p> <!--wp-compress-html no compression-->
感谢:http://www.mywpku.com/wp-html-compression.html












发表回复