wordpress非插件压缩网页源代码

引言:此文由子域名转移而来,因为细微强迫症和放弃子域名而不舍得完全丢弃,所以将会逐步第二次转移文章到主域名上来,二者主题(阿里白秀和D8)均来自大前端,追求完美的同时有一丝小懒,主题就不换了,D8主题用起来挺好。

wordpress seo优化不仅仅是为了优化百度关键词排名,简单的说,如果你的网站很轻便,浏览速度很快,那你的网站访问量就很可观,也就是说用户体验良好,那就需要进行wordpress各方面的优化。

从网页源代码角度,如果把其中不必要的空格都去掉,势必加载网页速度会有所提升,下面就来介绍wordpress非插件压缩网页源代码的方法:

很简单,只要把如下代码加入到function.php中即可:

  1. //wordpress非插件压缩网页源代码
  2. function wp_compress_html(){
  3.     function wp_compress_html_main ($buffer){
  4.         $initial=strlen($buffer);
  5.         $buffer=explode("<!--wp-compress-html-->"$buffer);
  6.         $count=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.="\n<!--压缩前的大小: $initial bytes; 压缩后的大小: $final bytes; 节约:$savings% -->";
  25.         return $buffer_out;
  26.     }
  27.     ob_start("wp_compress_html_main");
  28. }
  29. add_action('get_header', 'wp_compress_html');

除此之外,还有两款压缩网页源代码的插件:

请跳转这里->wordpress压缩网页源代码插件

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注