今天陌小雨给大家分享一个免插件 wordpress 压缩网页代码的方法,这也是加快网页加载速度的一个技巧,你值得拥有
代码:
//压缩 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');
将上述代码放到 functions.php 中>之前就可以了。
有时候我们会为网站添加一些特效,有些呢就会直接加到 footer.php 中,有些 js 被压缩了就会出错,或者没有效果,这个时候,我们只需要用下面代码将 js 代码包裹起来就好了。
<!--wp-compress-html--><!--wp-compress-html no compression--> 如果你加的 js 没有效果,就放到这里,这样前台就不会压缩他们了啦 <!--wp-compress-html no compression--><!--wp-compress-html-->
你可能对这些文章感兴趣:
- 虚拟主机纯代码缓存wordpress网站的办法,带清除缓存功能
- WordPress估计文章阅读时间
- WordPress发布文章时远程图片自动下载(免插件修复版)
- WordPress文章发布后自动同步到新浪微博(新增特色图片)
- WordPress只需3步将wp-clean-up插件集成到主题
- 万网主机启用smtp发送邮件
- WordPress插件推荐:SEO Friendly Images
- WordPress免插件插入文章索引/文章目录
- WordPress站点不得不做的SEO
- WordPress免插件实现七牛免费图床
- WordPress免插件制作网站地图(HTML+XML两个版本)
- WordPress免插件去除分类category
- 利用editplus删除WordPress代码中多余空行
- WordPress如何引用同一个目录下的所有php文件
如有疑问,请前往问答中心反馈!
反馈