wordpess压缩html代码优化教程

教程评论11,556字数 1340阅读4分28秒阅读模式

wordpess博客系统的html代码压缩优化教程,直接把下面代码丢到主题functions.php文件中,然后清理缓存打开首页点击查看源代码就可以看到效果了。wordpess压缩html代码优化教程

  1. //压缩html代码
  2. function wp_compress_html(){
  3.     function wp_compress_html_main ($buffer){
  4.         $initial=strlen($buffer);
  5.         $buffer=explode("<!--wp-compress-html-->", $buffer);
  6.         $countcount=count ($buffer);
  7.         for ($i = 0; $i <= $count; $i++){
  8.             if (stristr($buffer[$i], '<!--wp-compress-html no compression-->')) {
  9.                 $buffer[$i]=(str_replace("<!--wp-compress-html no compression-->", " ", $buffer[$i]));
  10.             } else {
  11.                 $buffer[$i]=(str_replace("\t", " ", $buffer[$i]));
  12.                 $buffer[$i]=(str_replace("\n\n", "\n", $buffer[$i]));
  13.                 $buffer[$i]=(str_replace("\n", "", $buffer[$i]));
  14.                 $buffer[$i]=(str_replace("\r", "", $buffer[$i]));
  15.                 while (stristr($buffer[$i], '  ')) {
  16.                     $buffer[$i]=(str_replace("  ", " ", $buffer[$i]));
  17.                 }
  18.             }
  19.             $buffer_out.=$buffer[$i];
  20.         }
  21.         $final=strlen($buffer_out);
  22.         $savings=($initial-$final)/$initial*100;
  23.         $savings=round($savings, 2);
  24.         $buffer_out.="<!--压缩前的大小: $initial bytes; 压缩后的大小: $final bytes; 节约:$savings% -->";
  25.     return $buffer_out;
  26. }
  27. if ( !is_admin() ) {
  28.         ob_start("wp_compress_html_main");
  29.     }
  30. }
  31. add_action('init', 'wp_compress_html');

我的微信
微信扫一扫
weinxin
我的微信
我的微信公众号
微信扫一扫
weinxin
我的公众号
 
  • 本文由 夏沫 发表于 2021年3月7日23:08:38
  • 除非特殊声明,本站文章均为原创,转载请务必保留本文链接

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:
确定

拖动滑块以完成验证