前言
七牛云是国内先进的云存储服务商,和百度云等云服务不一样,七牛云专门做外链存储以及内容的处理等等,普通用户免费额度充足,每个月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 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,如下图所示