WP酷 WP酷
  • 主题
  • 教程
  • 插件
  • 官方 QQ 群
  • 建站推荐
  • 联系
  • nicetheme® 奈思主题
  • 注册
    登录
立即登录
  • 请到 [后台->外观->菜单] 中设置菜单
首页 › WordPress 教程 › WordPress非插件实现静态资源CDN功能

WordPress非插件实现静态资源CDN功能

PCDotFan8年前

 题目可能有些扯远了…… 实际上我想表达的是既然我们用上了七牛的源站加速这一功能,不如再做到底——将缓存插件的CDN功能关掉,我们自己添加代码实现。方法也很简单,主要就是将静态资源地址更换为另外一个地址。(例如,所有的图片地址为www.mywpku.com更改为 https://www.mywpku.com/wp-content/uploads即可以使用所述代码) 不知道WP酷这样描述大家能不能听懂……

 

将本地图片地址替换为CDN地址

 添加至主题目录functions.php中:

define('CDN_HOST','//cdn.mywpku.com');
add_filter('the_content','z_cdn_content');
function z_cdn_content($content){
  return str_replace(home_url().'/wp-content/uploads', CDN_HOST.'/wp-content/uploads', $content);
  }
  add_filter('wp_get_attachment_url','z_get_attachment_url',10,2);
function z_get_attachment_url($url, $post_id){
  return str_replace(home_url(), CDN_HOST, $url);
  }

  注意define(‘CDN_HOST’,’//cdn.mywpku.com’); 需要替换为你自己的CDN地址。

 

将主题静态资源地址替换为CDN地址

添加至主题目录functions.php中:

add_filter('stylesheet_directory_uri','z_cdn_stylesheet_directory_uri',10,3);
function z_cdn_stylesheet_directory_uri($stylesheet_dir_uri, $stylesheet, $theme_root_uri) {
   return str_replace(home_url(), CDN_HOST, $stylesheet_dir_uri);
 }
add_filter('template_directory_uri','z_cdn_template_directory_uri',10,3);
function z_cdn_template_directory_uri($template_dir_uri, $template, $theme_root_uri)
{
   return str_replace(home_url(), CDN_HOST, $template_dir_uri);
}

 

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

@Via:http://dachanghe.com/wordpress-wuchajian-qiniu-upyun-cdn.html

define('FocusCDNHost','http://ehsren.com');//wordpress网站网址
define('FocusCDNRemote','http://cdn.ehsren.com');//cdn域名
define('FocusCDNIncludes','wp-content,wp-includes');//设置加速目录
define('FocusCDNExcludes','.php|.xml|.html|.po|.mo');//设置文件白名单
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.
  
function do_cdnrewrite_ob_start() {
$rewriter = new FocusCDNRewriteWordpress();
$rewriter->register_as_output_buffer();
}
add_action('template_redirect', 'do_cdnrewrite_ob_start');
  
class FocusCDNRewriteWordpress extends FocusCDNRewrite
{
function __construct() {
$excl_tmp = FocusCDNExcludes;
$excludes = array_map('trim', explode('|', $excl_tmp));
  
parent::__construct(
FocusCDNHost,
FocusCDNRemote,
FocusCDNIncludes,
$excludes,
!!FocusCDNRelative
);
}
public function register_as_output_buffer() {
if ($this->blog_url != FocusCDNRemote) {
ob_start(array(&$this, 'rewrite'));
}
}
  
}
  
class FocusCDNRewrite {
var $blog_url    = null;
var $cdn_url     = null;
var $include_dirs   = null;
var $excludes    = array();
var $rootrelative   = false;
  
function __construct($blog_url, $cdn_url, $include_dirs, array $excludes, $root_relative) {
$this->blog_url   = $blog_url;
$this->cdn_url    = $cdn_url;
$this->include_dirs  = $include_dirs;
$this->excludes   = $excludes;
$this->rootrelative  = $root_relative;
}
  
protected function exclude_single(&$match) {
foreach ($this->excludes as $badword) {
if (stristr($match, $badword) != false) {
return true;
}
}
return false;
}
  
protected function rewrite_single(&$match) {
if ($this->exclude_single($match[0])) {
return $match[0];
} else {
if (!$this->rootrelative || strstr($match[0], $this->blog_url)) {
return str_replace($this->blog_url, $this->cdn_url, $match[0]);
} else {
return $this->cdn_url . $match[0];
}
}
}
  
protected function include_dirs_to_pattern() {
$input = explode(',', $this->include_dirs);
if ($this->include_dirs == '' || count($input) < 1) {
return 'wp\-content|wp\-includes';
} else {
return implode('|', array_map('quotemeta', array_map('trim', $input)));
}
}
  
public function rewrite(&$content) {
$dirs = $this->include_dirs_to_pattern();
$regex = '#(?<=[(\"\'])';
$regex .= $this->rootrelative
? ('(?:'.quotemeta($this->blog_url).')?')
: quotemeta($this->blog_url);
$regex .= '/(?:((?:'.$dirs.')[^\"\')]+)|([^/\"\']+\.[^/\"\')]+))(?=[\"\')])#';
return preg_replace_callback($regex, array(&$this, 'rewrite_single'), $content);
}
  
}

 

 

  如果您并没有使用前面的“将本地图片地址替换为CDN地址”代码,还需补充一条:

define('CDN_HOST','//cdn.mywpku.com');
#CDN#functions.php#links#七牛#替换图片地址#缓存
6
分享

本站下载管理系统「dl.mywpku.com」因年久失修而永久关闭。若需获得往年主题,请至 WP酷的百度网盘(提取码: 5rbm) 自行搜索下载。默认解压密码为「www.mywpku.com」。

对您造成的不便,敬请谅解!

PCDotFan 站长
文章 435评论 859
相关文章
  • [6.0 更新中文汉化]极度流行的SEO插件 – WordPress SEO by Yoast
  • WordPress 新用户注册邮件链接提示「您的密码重设链接无效」
  • AMP – 为 WordPress 移动站点操作全球访问优化
  • 去除谷歌服务 – WordPress 国外主题全方位加速
  • 使用 WP-CLI 简化 WordPress 操作流程
  • WordPress 基础开发 – 一探 WordPress Hook 机制及示例应用
  • 优化静态资源 – WordPress 国外主题全方位加速
  • WordPress 实现微信登录
评论 (17)
再想想
  • 沛然博居

    博主的这篇文章解决了困扰我很久的问题。一直用各种CDN插件都不能全站静态资源CDN,用这个方法,竟然实现了。
    其实可以将这几段代码合并成一个php文件,然后 require 进functions.php,这样升级主题只需要增加一句代码就搞定了。

    7年前
  • 自来也

    define(‘CDN_HOST’,’//dn-wpku.qbox.me’),在添加这个域名的时候需要www这个吗?代码加上了,暂时没有反映,,

    6年前
    • PCDotFan

      @自来也 注意是CDN地址唷,只要该地址能访问就可以填写。

      6年前
  • qiang

    请问一下博主,【将 wp-content / wp-includes 静态资源替换为CDN地址】 按照博主的方法设置了,也生效了,但是.php文件也一同被替换了,白名单没有生效,博主能回复一下吗

    6年前
    • PCDotFan

      @qiang 抱歉,代码年久失修……直接使用 WP Super Cache 的 CDN 功能会更方便。

      6年前
PCDotFan
站长
若无特别注明,则默认我发布的所有文章均为内容原创 / 翻译原创,转载时请保留来源。
435文章
859评论
3K获赞
猜你喜欢
WordPress 终极加速系列 – 加速原理
6年前
去除谷歌服务 – WordPress 国外主题全方位加速
6年前
Typecho转WordPress步骤详解
10年前
  • 杂记
  • WordPress 付费主题推荐
Copyright © 2023 WP酷. Designed by nicetheme. 琼ICP备13002067号-1