首页 > 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. } 
  5. add_filter('template_directory_uri','z_cdn_template_directory_uri',10,3); 
  6. function z_cdn_template_directory_uri($template_dir_uri, $template, $theme_root_uri)
  7. { 
  8.    return str_replace(home_url(), CDN_HOST, $template_dir_uri); 
  9. } 

将 wp-content / wp-includes 静态资源替换为CDN地址.

  1. @Via:http://www.Vevb.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. } 
  13. add_action('template_redirect', 'do_cdnrewrite_ob_start'); 
  14.    
  15. class FocusCDNRewriteWordpress extends FocusCDNRewrite 
  16. { 
  17. function __construct() { 
  18. $excl_tmp = FocusCDNExcludes; 
  19. $excludes = array_map('trim', explode('|', $excl_tmp)); 
  20.    
  21. parent::__construct( 
  22. FocusCDNHost, 
  23. FocusCDNRemote, 
  24. FocusCDNIncludes, 
  25. $excludes, 
  26. !!FocusCDNRelative 
  27. ); 
  28. } 
  29. public function register_as_output_buffer() { 
  30. if ($this->blog_url != FocusCDNRemote) { 
  31. ob_start(array(&$this, 'rewrite')); 
  32. } 
  33. } 
  34.    
  35. } 
  36.    
  37. class FocusCDNRewrite { 
  38. var $blog_url    = null; 
  39. var $cdn_url     = null; 
  40. var $include_dirs   = null; 
  41. var $excludes    = array(); 
  42. var $rootrelative   = false; 
  43.    
  44. function __construct($blog_url, $cdn_url, $include_dirs, array $excludes, $root_relative) { 
  45. $this->blog_url   = $blog_url; 
  46. $this->cdn_url    = $cdn_url; 
  47. $this->include_dirs  = $include_dirs; 
  48. $this->excludes   = $excludes; 
  49. $this->rootrelative  = $root_relative; 
  50. } 
  51.    
  52. protected function exclude_single(&$match) { 
  53. foreach ($this->excludes as $badword) { 
  54. if (stristr($match, $badword) != false) { 
  55. return true; 
  56. } 
  57. } 
  58. return false; 
  59. } 
  60.    
  61. protected function rewrite_single(&$match) { 
  62. if ($this->exclude_single($match[0])) { 
  63. return $match[0]; 
  64. } else { 
  65. if (!$this->rootrelative || strstr($match[0], $this->blog_url)) { 
  66. return str_replace($this->blog_url, $this->cdn_url, $match[0]); 
  67. } else { 
  68. return $this->cdn_url . $match[0]; 
  69. } 
  70. } 
  71. } 
  72.    
  73. protected function include_dirs_to_pattern() { 
  74. $input = explode(',', $this->include_dirs); 
  75. if ($this->include_dirs == '' || count($input) < 1) { 
  76. return 'wp/-content|wp/-includes'; 
  77. } else { 
  78. return implode('|', array_map('quotemeta', array_map('trim', $input))); 
  79. } 
  80. } 
  81.    
  82. public function rewrite(&$content) { 
  83. $dirs = $this->include_dirs_to_pattern(); 
  84. $regex = '#(?<=[(/"/'])'; 
  85. $regex .= $this->rootrelative 
  86. ? ('(?:'.quotemeta($this->blog_url).')?') 
  87. : quotemeta($this->blog_url); 
  88. $regex .= '/(?:((?:'.$dirs.')[^/"/')]+)|([^//"/']+/.[^//"/')]+))(?=[/"/')])#'; 
  89. return preg_replace_callback($regex, array(&$this, 'rewrite_single'), $content); 
  90. } 
  91.    
  92. } 

wordpress免插件纯代码实现又拍云、七牛CDN加速

wordpress七牛镜像存储插件,WP SUPER CACHE等里面的CDN功能,都可以用代码方式实现,在七牛或者又拍云设置好CDN后,将下面代码仍入主题的functions.php函数文件中即可.

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表