国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > CMS > Wordpress > 正文

WordPress 非插件實現靜態資源CDN加速 及 又拍云、七牛CDN配置

2024-09-07 00:51:51
字體:
來源:轉載
供稿:網友

本文我們來分享一個 WordPress 非插件純代碼實現的靜態資源CDN加速的功能,文章后面我們再將告訴你如何在又拍云、七牛CDN加速配置.

先我們看看實現CDN加速功能的步驟,將本地圖片地址替換為CDN地址,添加至主題目錄functions.php中:

  1. define('CDN_HOST','http://cdn.mywpku.com'); 
  2. add_filter('the_content','z_cdn_content'); 
  3. function z_cdn_content($content){ 
  4.   return str_replace(home_url().'/wp-content/uploads', CDN_HOST.'/wp-content/uploads'$content); 
  5.   } 
  6.   add_filter('wp_get_attachment_url','z_get_attachment_url',10,2); 
  7. function z_get_attachment_url($url$post_id){ 
  8.   return str_replace(home_url(), CDN_HOST, $url); 
  9.   } 

注意 define('CDN_HOST','http://cdn.mywpku.com'); 需要替換為你自己的CDN地址,將主題靜態資源地址替換為CDN地址,添加至主題目錄functions.php中:

  1. add_filter('stylesheet_directory_uri','z_cdn_stylesheet_directory_uri',10,3); 
  2. function z_cdn_stylesheet_directory_uri($stylesheet_dir_uri$stylesheet$theme_root_uri) { 
  3.    return str_replace(home_url(), CDN_HOST, $stylesheet_dir_uri); 
  4. add_filter('template_directory_uri','z_cdn_template_directory_uri',10,3); 
  5. function z_cdn_template_directory_uri($template_dir_uri$template$theme_root_uri)
  6.    return str_replace(home_url(), CDN_HOST, $template_dir_uri); 

將 wp-content / wp-includes 靜態資源替換為CDN地址.

  1. @Via:http://www.survivalescaperooms.com/jianzhan/2282.html 
  2.  
  3. define('FocusCDNHost','http://ehsren.com');//wordpress網站網址 
  4. define('FocusCDNRemote','http://cdn.ehsren.com');//cdn域名 
  5. define('FocusCDNIncludes','wp-content,wp-includes');//設置加速目錄 
  6. define('FocusCDNExcludes','.php|.xml|.html|.po|.mo');//設置文件白名單 
  7. define('FocusCDNRelative','');//Check this if you want to have links like <wp-content/abc.png> rewritten - i.e. without your blog's domain as prefix. 
  8.    
  9. function do_cdnrewrite_ob_start() { 
  10. $rewriter = new FocusCDNRewriteWordpress(); 
  11. $rewriter->register_as_output_buffer(); 
  12. add_action('template_redirect''do_cdnrewrite_ob_start'); 
  13.    
  14. class FocusCDNRewriteWordpress extends FocusCDNRewrite 
  15. function __construct() { 
  16. $excl_tmp = FocusCDNExcludes; 
  17. $excludes = array_map('trim'explode('|'$excl_tmp)); 
  18.    
  19. parent::__construct( 
  20. FocusCDNHost, 
  21. FocusCDNRemote, 
  22. FocusCDNIncludes, 
  23. $excludes
  24. !!FocusCDNRelative 
  25. ); 
  26. public function register_as_output_buffer() { 
  27. if ($this->blog_url != FocusCDNRemote) { 
  28. ob_start(array(&$this'rewrite')); 
  29.    
  30.    
  31. class FocusCDNRewrite { 
  32. var $blog_url    = null; 
  33. var $cdn_url     = null; 
  34. var $include_dirs   = null; 
  35. var $excludes    = array(); 
  36. var $rootrelative   = false; 
  37.    
  38. function __construct($blog_url$cdn_url$include_dirsarray $excludes$root_relative) { 
  39. $this->blog_url   = $blog_url
  40. $this->cdn_url    = $cdn_url
  41. $this->include_dirs  = $include_dirs
  42. $this->excludes   = $excludes
  43. $this->rootrelative  = $root_relative
  44.    
  45. protected function exclude_single(&$match) { 
  46. foreach ($this->excludes as $badword) { 
  47. if (stristr($match$badword) != false) { 
  48. return true; 
  49. return false; 
  50.    
  51. protected function rewrite_single(&$match) { 
  52. if ($this->exclude_single($match[0])) { 
  53. return $match[0]; 
  54. else { 
  55. if (!$this->rootrelative || strstr($match[0], $this->blog_url)) { 
  56. return str_replace($this->blog_url, $this->cdn_url, $match[0]); 
  57. else { 
  58. return $this->cdn_url . $match[0]; 
  59.    
  60. protected function include_dirs_to_pattern() { 
  61. $input = explode(','$this->include_dirs); 
  62. if ($this->include_dirs == '' || count($input) < 1) { 
  63. return 'wp/-content|wp/-includes'
  64. else { 
  65. return implode('|'array_map('quotemeta'array_map('trim'$input))); 
  66.    
  67. public function rewrite(&$content) { 
  68. $dirs = $this->include_dirs_to_pattern(); 
  69. $regex = '#(?<=[(/"/'])'
  70. $regex .= $this->rootrelative 
  71. ? ('(?:'.quotemeta($this->blog_url).')?'
  72. : quotemeta($this->blog_url); 
  73. $regex .= '/(?:((?:'.$dirs.')[^/"/')]+)|([^//"/']+/.[^//"/')]+))(?=[/"/')])#'
  74. return preg_replace_callback($regexarray(&$this'rewrite_single'), $content); 
  75.    

wordpress免插件純代碼實現又拍云、七牛CDN加速

wordpress七牛鏡像存儲插件,WP SUPER CACHE等里面的CDN功能,都可以用代碼方式實現,在七牛或者又拍云設置好CDN后,將下面代碼仍入主題的functions.php函數文件中即可.

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 什邡市| 于田县| 呈贡县| 克什克腾旗| 合作市| 玉龙| 香港| 陈巴尔虎旗| 祁连县| 浪卡子县| 广昌县| 岐山县| 牡丹江市| 永平县| 胶南市| 司法| 苏尼特左旗| 金川县| 昭觉县| 淄博市| 肃北| 龙陵县| 龙南县| 汉沽区| 丰都县| 永和县| 温州市| 松潘县| 城步| 札达县| 泸州市| 仙桃市| 云阳县| 宁南县| 桑日县| 佛教| 鲁甸县| 海淀区| 淅川县| 横山县| 满城县|