2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

挿入する画像のパスをルートからのパスにする

Last updated at Posted at 2012-10-02

WordPress HTTPS プラグインで管理画面を https にすると、記事やページに挿入する画像のパスまで https になってしまい困ります。

以下のスクリプトを functions.php に。

functions.php
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');

サイトの移行の際にもパスを書き換えなくてすむので、便利です。

出典
http://www.warna.info/archives/20/

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?