但是壓縮頁面的好處就是減少了頁面的體積,從訪問速度上來說,更快些,盡管這些是肉眼看不到的,但是至少大叔喜歡這樣,另一方面就是給扒皮者制造麻煩,他們不得不重新整理頁面代碼的整潔性!
給wordpress頁面進行壓縮不是壓縮自己的實際代碼,而是前臺預覽的壓縮!相關插件是“WP-HTML-Compression” 但是今天不是說用插件來完成,再說,就目前,貌似WP-HTML-Compression和七牛有沖突,啟動WP-HTML-Compression后,會導致文件不被七牛所鏡像!
下面直接說今天的教程吧,打開wordpress主題文件里的functions.php,將下面的代碼貼入下面直接說今天的教程吧,打開wordpress主題文件里的functions.php,將下面的代碼貼入
PHP Code復制內容到剪貼板
- //壓縮html代碼
- 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');
壓縮頁面會衍生出一些問題,比如說,某些位置的某些特效失效了,那么我們還需要對某些位置進行禁止壓縮操作,方法是:
PHP Code復制內容到剪貼板
- <!--wp-compress-html--><!--wp-compress-html no compression-->
- 不被壓縮的部分
- <!--wp-compress-html no compression--><!--wp-compress-html-->
【網站聲明】本站除付費源碼經過測試外,其他素材未做測試,不保證完整性,網站上部分源碼僅限學習交流,請勿用于商業用途。如損害你的權益請聯系客服QQ:2655101040 給予處理,謝謝支持。