国内外主机
测评及优惠码

wordpress优化之网页代码压缩(代码版)

在以往的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');

为什么不使用插件,以及为什么要使用插件呢?不使用插件可以看这里,使用插件主要是方便移植!好了,今天的教程就到这里,快去试一试吧。友情提示:如果想要在网页源文件末尾查看压缩信息,请删除文中的注释!

  1. <!–wp-compress-html–><!–wp-compress-html no compression–>
  2. 不被压缩的部分
  3. <!–wp-compress-html no compression–><!–wp-compress-html–>
赞一个
赞(0)
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
文章名称:《wordpress优化之网页代码压缩(代码版)》
文章链接:https://www.ibytx.com/1253.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。

登录

找回密码

注册