WordPress:让站内资源使用相对路径

相对路径

好处:

  1. 引用站内资源,可以使加载速度更快一些;
  2. 更好的支持SSL(浏览器不会警告不安全信息);
  3. 别人复制数据直接粘贴,无法显示

Linux+Nginx+PHP+MySQL

插件和改代码

// 修改主题的functions.php 里面的clear_urlcan函数为下面.
// 建议先备份一下之前的.
function clear_urlcan($url){
    $rstr='';
    $tmparr=parse_url($url);
    //$rstr=empty($tmparr['scheme'])?'https://':$tmparr['scheme'].'://';
    //$rstr.=$tmparr['host'].$tmparr['path'];
    $rstr=$tmparr['path'];
    return $rstr;
}

然后安装插件:Root Relative URLs
安装之后,然后修改该插件的sb_root_relative_urls.php文件中的函数为下面这个:

static function scheme( $url ) {
        //And this here is a prime example of why absolute urls in WordPress create extra overhead and processing.
        //And in the core, they use four different approaches to acheive this translation!
        //For reference, see: http://core.trac.wordpress.org/ticket/19037
        //if (is_ssl()) {
            $url = str_replace('http://', 'https://', $url);
        //}
        //else {
          //  $url = str_replace('https://', 'http://', $url);
        // }
        return $url;
    }

然后就可以了..
如果不修改第一个,只安装并启用了插件,会有资源访问不上.

搞定.