WordPress不用插件使用七牛云存储加速网站

网站使用七牛云存储的好处?

加速网站的css、js、图片等内容,提升用户体验。

在我们运营网站时多多少少会用到图片存储,七牛云存储注册认证通过标准用户就可以得到10G的免费存储空间和免费10G的月流量,10G空间对于一般个人博客或小型站点基本上就够用了。

wordpress七牛云存储的插件也有很多,假如你不想用插件便可以使用七牛云存储加速网站,那么下面的一段代码可以帮你实现。


	//七牛镜像存储
	if ( !is_admin() ) {
		add_action(\'wp_loaded\',\'c7sky_ob_start\');
		function c7sky_ob_start() {
			ob_start(\'c7sky_qiniu_cdn_replace\');
		}
		function c7sky_qiniu_cdn_replace($html){
			$local_host = \'https://www.xxx.com\'; //你的网站域名  http或是https根据你的站点更换
			$qiniu_host = \'https://img.xxx.com\'; //你的七牛域名  http或是https根据你的站点更换
			$cdn_exts   = \'png|jpg|jpeg|gif\'; //扩展名(使用|分隔)
			$cdn_dirs   = \'wp-content|wp-includes\'; //目录(使用|分隔)
			$cdn_dirs   = str_replace(\'-\', \'\\-\', $cdn_dirs);
			if ($cdn_dirs) {
				$regex  =  \'/\' . str_replace(\'/\', \'\\/\', $local_host) . \'\\/((\' . $cdn_dirs . \')\\/[^\\s\\?\\\\\\\'\\\"\\;\\>\\<]{1,}.(\' . $cdn_exts . \'))([\\\"\\\\\\\'\\s\\?]{1})/\';
				$html =  preg_replace($regex, $qiniu_host . \'/$1$4\', $html);
			} else {
				$regex  = \'/\' . str_replace(\'/\', \'\\/\', $local_host) . \'\\/([^\\s\\?\\\\\\\'\\\"\\;\\>\\<]{1,}.(\' . $cdn_exts . \'))([\\\"\\\\\\\'\\s\\?]{1})/\';
				$html =  preg_replace($regex, $qiniu_host . \'/$1$3\', $html);
			}
			return $html;
		}
	}

使用方法

将上面代码添加到主题functions.php文件就行