概要説明
WordPressでは絶対パス(https://~)を使うように設計されているのですが一括で相対パス(/~)に変更したい時用のカスタマイズ。
functions.php
/*----------------------------------------------------
出力ソースの絶対パスを相対パスに書き換える
----------------------------------------------------*/
class relative_URI {
public function __construct() {
add_action('get_header', array(&$this, 'get_header'), 1);
add_action('wp_footer', array(&$this, 'wp_footer'), 99999);
}
protected function replace_relative_URI($content) {
$home_url = trailingslashit(get_home_url('/'));
$top_url = preg_replace( '/^(https?:\/\/.+?)\/(.*)$/', '$1', $home_url );
return str_replace( $top_url, '', $content );
}
public function get_header(){
ob_start(array(&$this, 'replace_relative_URI'));
}
public function wp_footer(){
ob_end_flush();
}
}
$relative_URI = new relative_URI();
絶対パスのほうが安定稼働
どうしても相対パスにしたいという場合以外は使用しないほうが良いかもしれません
問題となる例 – Pagespeed Insights



