wordpresの絶対パスを相対パスにする方法

wordpressはデフォルトで絶対パスですが、相対パスにする方法を記述します。

function.phpに記述

01class relative_URI {
02    public function __construct() {
03        add_action('get_header', array(&$this, 'get_header'), 1);
04        add_action('wp_footer', array(&$this, 'wp_footer'), 99999);
05    }
06    protected function replace_relative_URI($content) {
07        $home_url = trailingslashit(get_home_url('/'));
08        $top_url = preg_replace( '/^(https?:\/\/.+?)\/(.*)$/', '$1', $home_url );
09        return str_replace( $top_url, '', $content );
10    }
11    public function get_header(){
12        ob_start(array(&$this, 'replace_relative_URI'));
13    }
14    public function wp_footer(){
15        ob_end_flush();
16    }
17}
18$relative_URI = new relative_URI();

上記コードをfunction.phpに記述してアップすればいいです。この方法はモンキーレンチさんのWordPressで自ドメインのURLを相対パスに変換する方法で紹介されています。

個人支援・寄付について

サイトラボでは個人支援・寄付を受けております。ご協力いただける方はお願いいたします。当サイトではビットコインで受け付けております。

  • ビットコイン:3LHnADwZwUbic2L45EnVJEykiG6KfbqrwS