そのようなわけで、ライフログを取るにあたって、色々と初めの仕組み作りをしているわけですが、
ブログをWordPressで書こうとする場合、画像を添付する時は、画像の挿入リンクは、絶対リンクになる仕様です。
例えば、
<img src="http://khoshino.com/wp-content/uploads/2011/10/abc.jpg" />
となります。
この場合、例えば、ローカル環境や構築環境で作ったサイトを、本番用のドメインで公開する場合、画像を既に多くアップロードしている場合には、この画像のドメイン部分のURLを置換する必要があります。
また、既に運営しているサイトを、何かの事情でドメインを移行する場合にも、同様に、この画像のドメイン部分のURLを置換する必要が生じるでしょう。
以上のような理由から、WordPressで挿入する画像のsrcを相対リンクにする需要があるわけで、大曲さんが、ソースコードを書いてくださっています。
このソースコードを用いると、
WordPressで挿入する画像のsrcを相対リンクにするでは、
<img src="/wp-content/uploads/2011/10/abc.jpg" />
となります。
また、
WordPressで挿入する画像のsrcを相対リンクにする【発展版】では、
<img src="[domain]/wp-content/uploads/2011/10/abc.jpg" />
となり、結果的には、
<img src="http://khoshino.com/wp-content/uploads/2011/10/abc.jpg" />
という感じで、絶対パスになるけど、柔軟に対応できるという仕様になっています。
大曲さんの提案は、テーマのfunctions.phpの中に、ソースコードを書く、という方法です。
もちろん、この方法でも問題無いのですが、すると、テーマを変えるたびに、コードをまたfunctions.phpに書かなくてはいけません。
一方で、プラグイン化すれば、そのサイトにおいては、テーマを変えても、ずっと相対リンクで画像を表示することができます。
また、大曲さんの【発展版】の方の場合、ショートコードを使っているので、このソースコードを有効化しなくなった際には、画像が表示されなくなってしまうと思います。そのような理由から、プラグイン化で、このブログでは使ってみようかと思いました。
WordPressで挿入する画像のsrcを相対リンクにする【発展版】 | Simple Colors
<?php /* Plugin Name: gazou src Plugin URI: http://wp3.jp/ Description: WordPressで挿入する画像のsrcを相対リンクにします。 Author: Kunitoshi Hoshino Version: 0.1 Author URI: http://wp3.jp/ */ /* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ /* このプラグインは、大曲さんの「WordPressで挿入する画像のsrcを相対リンクにする【発展版】」をプラグイン化しただけのものです。 プラグイン化した理由は、テーマのfunctions.phpに記載した場合、テーマを変えるたびに、コードをまたfunctions.phpに書かなくてはいけません。 プラグイン化すれば、そのサイトにおいては、テーマを変えても、ずっと相対リンクで画像を表示できるからです。 素敵なソースコードを書いてくださった大曲さんに感謝します! WordPressで挿入する画像のsrcを相対リンクにする【発展版】 | Simple Colors URL: http://www.warna.info/archives/1789/ */ function delete_host_from_attachment_url( $url ) { $regex = '/^http(s)?:\/\/[^\/\s]+(.*)$/'; if ( preg_match( $regex, $url, $m ) ) { $url = '[domain]' . $m[2]; } return $url; } add_filter( 'wp_get_attachment_url', 'delete_host_from_attachment_url' ); function domain_shortcode() { if ( preg_match( '|^https?://[^/]+|', get_option( 'home' ), $m ) ) { $domain = $m[0]; } else { $domain = ''; } return $domain; } add_shortcode( 'domain', 'domain_shortcode' );
なお、ショートコードを使わず、純粋な相対リンクで十分だよ、という人は、コチラです。
WordPressで挿入する画像のsrcを相対リンクにする | Simple Colors
<?php /* Plugin Name: gazou src Plugin URI: http://wp3.jp/ Description: WordPressで挿入する画像のsrcを相対リンクにします。 Author: Kunitoshi Hoshino Version: 0.1 Author URI: http://wp3.jp/ */ /* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ /* このプラグインは、大曲さんの「WordPressで挿入する画像のsrcを相対リンクにする」をプラグイン化しただけのものです。 プラグイン化した理由は、テーマのfunctions.phpに記載した場合、テーマを変えるたびに、コードをまたfunctions.phpに書かなくてはいけません。 プラグイン化すれば、そのサイトにおいては、テーマを変えても、ずっと相対リンクで画像を表示できるからです。 素敵なソースコードを書いてくださった大曲さんに感謝します! WordPressで挿入する画像のsrcを相対リンクにする | Simple Colors URL: http://www.warna.info/archives/20/ */ function delete_host_from_attachment_url( $url ) { $regex = '/^http(s)?:\/\/[^\/\s]+(.*)$/'; if ( preg_match( $regex, $url, $m ) ) { $url = $m[2]; } return $url; } add_filter( 'wp_get_attachment_url', 'delete_host_from_attachment_url' ); add_filter( 'attachment_link', 'delete_host_from_attachment_url' );
以上のいずれかのソースコードを、gazou-src.phpなど、適当なファイルを作って、[wp-content]の階層下の[plugins]の中に、アップロードしましょう。
ただ、1点、WordPressが公式に出しているiPhoneアプリから画像を投稿をすると、結局、画像は、絶対パスになるので、iPhoneアプリやメール投稿から、画像も投稿する人は、結局、MySQL(データベース)からの一括置換が必要になる気がしますねぇ。