WordPress纯代码实现前端页面HTML完美压缩

简介前言:压缩HTML页面的好处就是略微提升页面加载速度,并给那些爱扒皮的缺德玩意制造些许麻烦。好了将如下代码添加在functions.php中即可://压缩WordPress前端html代码functionwp_compress_html(){functionwp_compress_html_main($buffer){$initial=strlen($buffer);$buffer=explode

  

前言:压缩HTML页面的好处就是略微提升页面加载速度,并给那些爱扒皮的缺德玩意制造些许麻烦。

好了将如下代码添加在 functions.php 中即可:

//压缩WordPress前端html代码functionwp_compress_html(){functionwp_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-htmlnocompression-->')){$buffer[$i]=(str_replace("<!--wp-compress-htmlnocompression-->","",$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<!--压缩前的大小:$initialbytes;压缩后的大小:$finalbytes;节约:$savings%-->";return$buffer_out;}ob_start("wp_compress_html_main");}add_action('get_header','wp_compress_html');//当检测到文章内容中有代码标签时文章内容不会被压缩functionunCompress($content){if(preg_match_all('/(crayon-|</pre>)/i',$content,$matches)){$content='<!--wp-compress-html--><!--wp-compress-htmlnocompression-->'.$content;$content.='<!--wp-compress-htmlnocompression--><!--wp-compress-html-->';}return$content;}add_filter("the_content","unCompress");

下载链接:网站源码/小程序源码/网站模板下载

版权声明

   站内部分内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供网络资源分享服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请 联系我们 一经核实,立即删除。并对发布账号进行永久封禁处理。在为用户提供最好的产品同时,保证优秀的服务质量。


本站仅提供信息存储空间,不拥有所有权,不承担相关法律责任。

给TA打赏
共{{data.count}}人
人已打赏
网站技巧

PHP调用今天的日期几月星期几

2024-9-19 19:44:20

网站技巧

帝国CMS实现tagid伪静态tag的方法

2024-9-19 19:44:22

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索