WordPress优化加速压缩网站前端代码教程

使用代码方法实现WordPress前端代码压缩,压缩页面的好处就是减少了页面的体积,从访问速度上来说,更快些,尽管这些是很难感觉出来的,但是当你流量大了就能有感觉了,这样还可以给copy的用户制造麻烦让他一看你源代码就想关闭!

WordPress优化加速:网站前端代码压缩教程分享

将以下代码放到当前使用主题的functions.php文件即可,不过由于代码比较多个人还是推荐单独创建一个PHP文件然后在functions.php文件里面调用即可。

比如创建一个html.php文件将以下代码复制进去,然后上传到当前使用的主题文件夹里面,然后在functions.php文件里面添加一段include('html.php');代码进行调用。

// html代码压缩
<?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("\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-->
不被压缩的部分
<!--wp-compress-html no compression--><!--wp-compress-html-->

WordPress之防御cc攻击[频繁F5刷新]的解决方法

上一篇

未备案的域名怎么接入QQ互联平台免备案接入QQ互联

下一篇

99%的人还看了

发表评论

插入图片

欢迎登陆本站

 | 注册

在线工具

VIP视频解析

衡天云优质VPS 站长推荐

注册

登录

忘记密码 ?