以前介紹過使用七牛雲加速,自我感覺七牛還是蠻不錯的。 當然又拍雲也可以啦!
關於七牛雲端儲存的好處我們不多說了,它可以把我們網站的 css.js. 圖片等全部放在七牛進行加速
雖然有免費流量的限制但是小站基本上夠用,幻殺使用後感覺速度提升還是很明顯的。
幻殺使用的外掛,是我愛水煮魚開發的七牛雲端儲存外掛,但是使用 P3(點選這裡檢視) 進行檢測時,顯示居然很坑速度,本來我還是很喜歡外掛,但是對速度好吧……我不說了。
嗯,這個方法你可以選擇用程式碼的,當然也可以不用程式碼的,如果都嫌煩那麼還是用外掛吧 關於
多方法可以選擇性共同使用,記住是選擇 !選擇喲!記住看最後的評論再選擇!

方法 1:
把這個外掛程式碼化,這樣就不用外掛了。 開啟你主題檔案中的 funsion 檔案 (修改有風險,用前需備份) 加入以下程式碼
//將主題自帶的 CSS 和 JS 檔案替換成七牛映象儲存
define(『CDN_HOST』,』http://hsdate.qiniudn.com/』);
add_filter(『stylesheet_directory_uri』,』dw_cdn_stylesheet_directory_uri』,10,3);
function dw_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』,』dw_cdn_template_directory_uri』,10,3);
function dw_cdn_template_directory_uri($template_dir_uri, $template, $theme_root_uri) {
return str_replace(home_url(), CDN_HOST, $template_dir_uri);
把裡面設定的 hsdate.qiniudn.com 地址換成你自己申請的加速域名。七牛又拍應該都有吧!
然後儲存重新整理下後檢視下原始碼,發現有關 js 的域名是不是已經被更改了!
當然這個只可以對你的 js 和 css 進行加速,對圖片無效!
方法 2:
使用上邊方法加速效果不好?一般大家站點 js css 數量應該沒有圖片多,最主要還是加速圖片吧
使用這個方法圖片也是可以加速的。
所以那使用下面的方法就可以將圖片也使用七牛雲端儲存了。
就如這個地址:http://ihuan.me/wp-admin/options.php,然後那找到 upload_url_path,使用快捷鍵 跟方便 別自個憑眼睛慢慢找,你會瘋的!
其中 ihuan.me 要換成你自己個部落格地址 如果安裝在子目錄下 將子目錄一塊弄進去。
找到後 在那項出 新增 http://hsdate.qiniudn.com/wp-content/uploads,然後儲存,其中 hsdate.qiniudn.com 一樣換成你自己的。
這時候我們寫一篇文章新增一張圖片試試,圖片的地址是不是已經變為七牛的地址了呢!
方法 2 有缺陷:就是說圖片直接存在七牛的 cdn 空間上,這個對於網站空間緊張的使用者來說是一個不錯的選擇
但是對於網站空間很充足的來說就不是很爽了,因為七牛畢竟不是自己的空間,我們不可能吧希望全部放在七牛那邊,將來如果更換 cdn 空間的話這些圖片有可能就全部無法使用!
簡單的說,就是圖片將直接上傳至七牛空間而不透過你自己的空間! 圖片不會在你的空間儲存而是直接儲存到了七牛!有利有弊,空間小的高興,空間足的不高興!
因為方法 2 沒有備份,如果你有需要更換的話,就選擇方法 3,圖片在本地回自動保留一份!
方法 3:
define(『FocusCDNHost』,』http://ihuan.me』);//WordPress 網站網址
define(『FocusCDNRemote』,』http://hsdate.qiniudn.com』);//cdn 域名
define(『FocusCDNIncludes』,』wp-content,wp-includes』);//設定加速目錄,可自行輸入
define(『FocusCDNExcludes』,』.php|.xml|.html|.po|.mo』);//設定檔案白名單,一樣可自行輸入
define(『FocusCDNRelative』,」);
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);
}
}
方法 3 很高大上,完全可以和外掛媲美,準確說和外掛差不多了,外掛有的他也有,除了七牛那個噁心人的水印木有!
白名單啥的都有了!
根據前面的描述,大體給個使用的說明
方法一,可以只可以加速 js css
方法二,可以加速圖片等透過上傳方式上傳的檔案,但是缺點是檔案沒有在本地進行備份而是直接去了七牛!
方法三,可控可存 可以控制檔案儲存白名單,檔案也會在本地有儲存!
你可以依據此說明進行一下選擇,選擇喲,出錯咱不管!!!
好了,又可以幹掉一個外掛,麻麻再也不用怕外掛多了!