久青草国产观看在线视频,在线观看欧美日女,777毛片,亚洲国产精品99久久久久久久

wordpress優雅的引入MIP,加速手機端訪問速度

2017-08-12 wordpress教程
  • 文章介紹
  • 快速入門
  • 評價&建議

百度站長工具推出以后沒有怎么關注,直到用時,才發現這手機端的速度優化真的不是蓋的。

什么是MIP?

MIP (Mobile Instant Pages - 移動網頁加速器), 是一套應用于移動網頁的開放性技術標準。通過提供 MIP-HTML 規范、MIP-JS 運行環境以及 MIP-Cache 頁面緩存系統,實現移動網頁加速。

MIP 主要由三部分組織成:

  • MIP HTML
  • MIP JS
  • MIP Cache

MIP HTML 基于 HTML 中的基礎標簽制定了全新的規范,通過對一部分基礎標簽的使用限制或功能擴展,使 HTML 能夠展現更加豐富的內容;MIP JS 可以保證 MIP HTML 頁面的快速渲染;MIP Cache 用于實現 MIP 頁面的高速緩存,從而進一步提高頁面性能。

WordPress 如何應用 MIP ?

首先,你要區分一下自己的主題是自適應的還是pc手機分離的。這邊推薦選擇PC和手機端分離的模式,為什么?

  • 方便改造代碼結構;
  • MIP禁止引入基于jQ的效果,如果你網站效果很多,那引入后會非常蛋疼。

然后,我們來看頭部使用規范

  • 起始標簽使用 <!doctype html>
  • html 標簽必須加上 mip 標記,即: <html mip>
  • 必須包含 <head>和 <body>標簽
  • 必須在 head 標簽中包含字符集聲明: <meta charset="utf-8">,字符集統一為utf-8
  • 必須在 head 標簽中包含 viewport 設置標簽: <meta name="viewport" content="width=device-width,initial-scale=1">,推薦包含minimum-scale=1
  • 必須在 head 標簽中包含 < link rel="stylesheet" type="text/css" >
  • 必須在 body 標簽中包含 <script src="https://mipcache.bdstatic.com/static/v1/mip.js" ></script >
  • 必須在 head 標簽中包含 <link rel="canonical" href="http(s)://xxx" >

案例:

  1. <!DOCTYPE html>  
  2. <html mip>  
  3. <!-- 注意html標簽后面要跟mip-->  
  4. <head>  
  5.   <meta charset="UTF-8">  
  6.   <meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">  
  7.   <title>MIP改造案例(你原本手機端主題的Title部分)</title>  
  8.   <meta name="description" content="" />  
  9.   <meta name="keywords" content="" />  
  10.   <link rel="stylesheet" type="text/css" href="https://mipcache.bdstatic.com/static/v1/mip.css">  
  11. <!-- 添加canonical屬性開始-->  
  12.   <?php  
  13.     if(is_home()){  
  14.         echo '<link rel="canonical" href="'.get_bloginfo('url').'" />'."\n";  
  15.     }else  
  16.     if(is_tax() || is_tag() || is_category()){  
  17.         $term = get_queried_object();  
  18.         echo '<link rel="canonical" href="'.get_term_link( $term, $term->taxonomy ).'" />'."\n";  
  19.     }else  
  20.     if(is_page()){  
  21.         echo '<link rel="canonical" href="'.get_permalink().'" />'."\n";  
  22.     }else  
  23.     if(is_single()){  
  24.         echo '<link rel="canonical" href="'.get_permalink().'" />'."\n";  
  25.     }  
  26.   ?>  
  27. <!-- 添加canonical屬性結束-->  
  28.   <style mip-custom>  
  29.   //你的style.css需要全部寫在這里,除了頭部,不允許其他地方再出現style屬性  
  30.   </style>  
  31. </head>  

 

頭部改造好以后,我們基本上就只需要改造一下頁面內容里的img標簽就可以了。

即:img 替換為 mip-img

接下來,是底部的改造:

底部我們只需要將js屬性添加上就可以了:

  1. <mip-stats-baidu token="xxxxx"></mip-stats-baidu>  
  2. <!--  
  3. // 例:百度統計代碼截取hm.src = "https://hm.baidu.com/hm.js?02890d4a309827eb62bc3335b2b28f7f";  
  4. // hm.js? 后為你的統計 token。此例 token="02890d4a309827eb62bc3335b2b28f7f"  
  5. -->  
  6. <script src="https://mipcache.bdstatic.com/static/v1/mip.js"></script>  
  7. <!--引入mip的菜單組件-->  
  8. <script src="https://mipcache.bdstatic.com/static/v1/mip-nav-slidedown/mip-nav-slidedown.js"></script>    
  9. <!--引入mip的百度統計組件-->  
  10. <script src="https://mipcache.bdstatic.com/static/v1/mip-stats-baidu/mip-stats-baidu.js"></script>  

接下來,給大家一個菜單導航的案例:

  1. <div class="mip-nav-wrapper show top-toolbar">  
  2.     <mip-nav-slidedown data-id="bs-navbar" class="mip-element-sidebar container mip-element mip-layout-container" data-showbrand="1" data-brandname="股市百科">  
  3.         <nav id="bs-navbar" class="" >  
  4.             <?php wp_nav_menu(array('theme_location'=>'header','container' => false,'menu_class'=>'nav navbar-nav navbar-right','depth'=>'0','echo'=>true)); ?>  
  5.         </nav>  
  6.     </mip-nav-slidedown>  
  7. </div>  

那么,有同學注意到了,文章內容里面的img標簽、style屬性沒有辦法處理,可以復制下面代碼進行處理,放入function文件里:

  1. //整理內容,代碼來源https://zhangzifan.com  
  2. add_filter('the_content', 'fanly_mip_images');  
  3. function fanly_mip_images($content){  
  4.     //WordPress文章內圖片適配百度MIP規范  
  5.     preg_match_all('/<img (.*?)\>/', $content$images);  
  6.     if(!is_null($images)) {  
  7.         foreach($images[1] as $index => $value){  
  8.             $mip_img = str_replace('<img', '<mip-img', $images[0][$index]);  
  9.             $mip_img = str_replace('>', '></mip-img>', $mip_img);  
  10.             //以下代碼可根據需要修改/刪除  
  11.             $mip_img = preg_replace('/(width|height)="\d*"\s/', ''$mip_img );//移除圖片width|height  
  12.             $mip_img = preg_replace('/ style=\".*?\"/', '',$mip_img);//移除圖片style 
  13.             //$mip_img = preg_replace('/ class=\".*?\"/', '',$mip_img);//移除圖片class 
  14.             //以上代碼可根據需要修改/刪除 
  15.             $content = str_replace($images[0][$index], $mip_img, $content); 
  16.         } 
  17.     } 
  18.     //WordPress文章內樣式去除 
  19.     preg_match_all('/ style=\".*?\"/', $content, $style); 
  20.     if(!is_null($style)) { 
  21.         foreach($style[0] as $index => $value){ 
  22.             $mip_style = preg_replace('/ style=\".*?\"/', '',$style[0][$index]);//移除文章內容中的style  
  23.             $content = str_replace($style[0][$index], $mip_style$content);  
  24.         }  
  25.     }  
  26.   
  27.     return $content;  
  28. }  

相對于,MIP也有主動推送功能,代碼也同時分享:

  1. //整理內容,代碼來源https://zhangzifan.com  
  2. add_action('save_post', 'fanly_mip_notify_baidu_zz', 10, 3);  
  3. function fanly_mip_notify_baidu_zz($post_id$post$update){  
  4.     if($post->post_status != 'publish') return;  
  5.    
  6.     $baidu_zz_api_url = 'http://data.zz.baidu.com/urls?site=xxx&token=xxxs&type=mip';  
  7.     //請到 百度站長后臺>移動專區>MIP引入>數據提交>主動推送(實時),復制接口調用地址  
  8.    
  9.     $response = wp_remote_post($baidu_zz_api_urlarray(  
  10.         'headers' => array('Accept-Encoding'=>'','Content-Type'=>'text/plain'),  
  11.         'sslverify' => false,  
  12.         'blocking' => false,  
  13.         'body' => get_permalink($post_id)  
  14.     ));  
  15. }  

代碼改造至此,就基本結束了。

我的演示:

手機訪問:http://www.gsbaike.com/

可以多打開幾個頁面,你會發現,響應速度是很快的,而且,這個網站是搭建在美國機子上的哦。

如果你的網站也想改造,而自己不會,可以聯系主題貓幫你改造哈。

 

1 1

企業建站推薦正版商業主題,國內專業團隊開發,完善售后,是您不二選擇。

正版主題商店

主題貓WP建站,累計幫助1300+客戶成功建站,為站長提供支持!

立刻開啟你的建站之旅
QQ在線客服

服務熱線

wordpress建站咨詢