| 
教程介绍 使用代码方法实现WordPress前端代码压缩,压缩页面的好处就是减少了页面的体积,从访问速度上来说,更快些,尽管这些是很难感觉出来的,但是当你流量大了就能有感觉了,这样还可以给扒皮的用户制造麻烦让他一看你源代码就想关闭! 代码预览将以下代码放到当前使用主题的functions.php文件即可,不过由于代码比较多个人还是推荐单独创建一个PHP文件然后在functions.php文件里面调用即可。比如创建一个cleloo.com.php文件将以下代码复制进去,然后上传到当前使用的主题文件夹里面,然后在functions.php文件里面添加一段include(‘cleloo.com.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-->
 
  复制代码 
 
 
 | 
共 0 个关于本帖的回复 最后回复于 2015-4-29 16:27