4
4

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.

画像URLを絶対パスから相対パスにする

Posted at

絶対パスの記述って好きじゃないので。。

functions.php
/**
 * 画像パスの相対化
 * @param $url 画像パス
 */
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');
4
4
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
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?