吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3362|回复: 3
收起左侧

[其他转载] 【搬砖】WordPress优化加速:前端代码压缩教程

[复制链接]
Syuxi 发表于 2016-8-16 20:13
教程介绍

使用代码方法实现WordPress前端代码压缩,压缩页面的好处就是减少了页面的体积,

从访问速度上来说,更快些,尽管这些是很难感觉出来的,但是当你流量大了就能有感觉了,

这样还可以给扒皮的用户制造麻烦让他一看你源代码就想关闭!

代码预览

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

比如创建一个html.php文件将以下代码复制进去,然后上传到当前使用的主题文件夹里面,然后在functions.php文件里面添加一段include('ymjihe.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-->









免费评分

参与人数 1热心值 +1 收起 理由
滑稽大表哥 + 1 最新的版本不是调用的本地字体么

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

Ademi 发表于 2016-8-16 20:18
搬?、、、这都可以搬。。厉害了
 楼主| Syuxi 发表于 2016-8-16 20:20
逆天昊龙 发表于 2016-8-16 20:22
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-9-23 11:28

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表