为WordPress添加七牛云存储功能

建站相关 myhloli 9878次浏览 已收录 13个评论

文章转自googlo.me


wordpress-qiniu-cdn

前言

七牛云是国内先进的云存储服务商,和百度云等云服务不一样,七牛云专门做外链存储以及内容的处理等等,普通用户免费额度充足,每个月10G流量,10G存储空间,对于开发者来说,七牛云简直就是太可爱了。

七牛云

七牛云的注册,创建空间过程等等就不多说了,网上很多的,说几个重点。
1,创建七牛帐号之后,默认是体验用户,及时绑定手机,升级为标准用户,否则可能会向你收费的。
2,添加域名的时候,绑定与否其实无所谓,不一定需要绑定。
3,添加域名应该和你的WordPress程序安装目录一致。

七牛插件方法

1,七牛插件个人推荐使用水煮鱼的七牛云存储插件,具体使用插件作者以及给出了详细的使用方法了

最详细七牛云存储插件使用方法

2,如果你只是想把附件,图片等存储在七牛这个纯粹的功能的话,qiniu cloud upload插件可能也是你的最爱,这个插件相对来说就比较小巧多了,安装之后配置插件完毕,之后插入图片什么的就直接到了七牛。
另外这个插件有一个独有的功能,就是能将七牛空间的文件批量下载到自己的本地空间,如果需要批量下载七牛空间文件的可以试试这款插件

注意,使用这款插件首先必须要在七牛空间上传一个【qiniu_test.jpg】的文件,用作验证之用。

 

安装:后台搜索安装即可

七牛代码方法

如果你不想使用插件,想使用代码解决,那么下面这段代码你可以试试

 

//七牛云储存代码
    define('FocusCDNHost','http://googlo.me');//wordpress网站网址
    define('FocusCDNRemote','http://googlo.me/wp-content/uploads');//cdn域名
    define('FocusCDNIncludes','wp-content,wp-includes,avater');//设置加速目录
    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);
    }
    }
 
//end

代码老规矩加入functions.php即可,代码里的东西改成自己的。


七牛路径替换方法

其实使用WordPress自带的功能也可以实现七牛上传文件的,只需要将文件完整路径替换成七牛的路径。
不过目前该功能已经被新版WordPress隐藏了,不过输入http://博客地址/wp-admin/options.php还是可以在这里启用的,使用ctrl+F功能寻找upload_url_path,然后再在输入框输入http://你的二级域名(七牛三级域名)/wp-content/uploads,如下图所示

2014-09-07_093207

 相关链接

七牛注册地址
  萝莉社,版权所有丨如未注明,均为原创丨本网站采用BY-NC-SA协议进行授权,转载请注明转自:https://myhloli.com/wordpress-qiniu.html
喜欢 (5)
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
(13)个小伙伴在吐槽
  1. 恩恩饿呢,不错不错
    空木君2015-12-19 21:44 回复
  2. 好!!!!!!!
    诗蓝2015-12-18 22:25 回复
  3. 固定连接枯了
    云落2015-02-27 12:36 回复
    • 链接已改,云落大大加个友链么
      myhloli2015-02-27 13:52 回复
  4. 。。。其实SAE只要改改代码就行了
    win7专家322015-02-23 21:30 回复
    • 别提sae- -,没有写入权限都是耍流氓
      myhloli2015-02-23 21:48 回复
      • 但是,在国内,免费,只需要认证
        win7专家322015-02-23 22:24 回复
        • 我有中级开发者认证- -,没啥用,搭wordpress不舒服,弄了个签到站就没啥别的用了
          myhloli2015-02-23 22:42 回复
          • 可是我就需要一个可以cron的应用平台。。。
            win7专家322015-02-23 22:51
          • sae简直不适合搭博客,当应用平台还算可以
            myhloli2015-02-23 22:54
          • 不好拿公家资源搭博客www,会被骂的。。。所以只能sae了。。。
            win7专家322015-02-23 22:57
          • 博客整体定型了用sae也凑合,但是我这种刚开始折腾的,天天都要修改很多文件,用sae能要老命了
            myhloli2015-02-23 23:00
          • 毕竟要磨合嘛
            win7专家322015-02-23 23:08
加载中……