压缩页面HTML代码,加速网页访问
压缩wordpress页面后对查看源代码来说不太友好,但是压缩页面的好处就是减少了页面的体积,从访问速度上来说,更快些,尽管这些是肉
眼看不到的,但Jay同学这么做的原因另一方面是避免查看页面源码扒站,当然内行的站长办法也多的是,这里只是给扒站者制造一些小麻烦
因为给wordpress页面进行压缩不是压缩自己的实际代码,而是前台预览的压缩!
实现压缩页面代码的方法也可以使用插件,当然这里还是本着尽量不安装插件的方法;很简单,直接打开wordpress主题文件里的
functions.php,将一下代码插入?> 之前即可
//WordPress代码压缩
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');
酷小呵
哥,我是酷小呵,我的域名换成了这个新的,麻烦给改一下呗,谢谢
https://www.kuhehe.top/
Jay
@酷小呵 okk,已更新