WordPress Logo WordPress のURL URI パスを取得・出力する関数

HTML ファイルの場合、通常は画像や他のファイルへのパスはそのファイルが置いてある場所から相対的な場所(位置)を指定しますが、WordPress では指定の方法が異なります。

静的な HTML ファイルの場合はファイルの位置はその構造に一致しているので相対的なパスで指定できますが、WordPress の場合は URL の構造とファイルの物理的な構造が一致しているわけではないので、 URL やパスを取得するにはテンプレートタグを使います。

関連ページ:「WordPress のフォルダ構成/テーマのディレクトリへのパス

更新日:2022年04月06日

作成日:2019年01月18日

テーマ内ファイル

現在有効になっているテーマのフォルダ内にあるファイルの URI(URL) や パスを取得する関数。

get_theme_file_uri

現在有効なテーマフォルダ内にあるファイルの URL を取得します。WordPress 4.7 から利用可能です。

get_theme_file_uri( $file )

パラメータ
$file (string):(オプション)ファイル名。初期値:""(空)
戻り値
テーマ内にあるファイルの URL。ファイル名が空の場合は、get_stylesheet_directory_uri() で取得する URL。
パラメータを指定しない場合の出力例
<?php 
  $uri = get_theme_file_uri(); 
  echo $uri;
?>

//テーマ名が「my-theme」の場合出力例(末尾にスラッシュなし)
http://example.com/wp-content/themes/my-theme

従来は、親テーマと子テーマのディレクトリの URL を取得する場合、以下のように使い分ける必要がありました。

get_theme_file_uri() を使うと、指定されたファイルが子テーマに存在すればその URL を、子テーマにファイルが存在しなければ親テーマの指定されたファイルの URL を返します。

※同名のファイルが親テーマと子テーマに存在すると子テーマ側のファイルが優先されます。

必ず親テーマのファイルの URL を取得したい場合は、get_parent_theme_file_uri() を使います。

この関数は以下のように「wp-includes/link-template.php」で定義されています。

  • ファイル名を指定しない(空の)場合は、get_stylesheet_directory_uri() で取得される値が返されます。(4~5行目)

  • file_exists で子テーマのディレクトリ内に指定されたファイルが存在するかを調べ、あればそのファイルの URL を設定します。(6~7行目)

  • 子テーマのディレクトリ内に指定されたファイルが存在しなければ、親テーマのディレクトリにファイルを指定して URL を設定します(ファイルが存在するかどうかは確認しません)。(9行目)

  • URL を返す前に theme_file_uri フィルターを呼び出します。

function get_theme_file_uri( $file = '' ) {
  $file = ltrim( $file, '/' );

  if ( empty( $file ) ) {
    $url = get_stylesheet_directory_uri();
  } elseif ( file_exists( get_stylesheet_directory() . '/' . $file ) ) {
    $url = get_stylesheet_directory_uri() . '/' . $file;
  } else {
    $url = get_template_directory_uri() . '/' . $file;
  }

  /**
   * Filters the URL to a file in the theme.
   *
   * @since 4.7.0
   *
   * @param string $url  The file URL.
   * @param string $file The requested file to search for.
   */
  return apply_filters( 'theme_file_uri', $url, $file );
}

images/fooフォルダ内の sample.png を読み込む場合の例

<img src="<?php echo get_theme_file_uri('/images/foo/sample.png'); ?>" alt="...">

以下はテーマ内の 「css」フォルダにある style.css を wp_enqueue_style() を使って読み込む例です。

function my_styles() {
  wp_enqueue_style('my-style', get_theme_file_uri('/css/style.css'));
}
add_action( 'wp_enqueue_scripts', 'my_styles' );

get_theme_file_path

現在有効なテーマフォルダ内にあるファイルの絶対パスを返します。末尾にスラッシュは含まれません。

get_theme_file_path( $file )

パラメータ
$file (string):(オプション)ファイル名。初期値:""(空)
戻り値
テーマ内にあるファイルの絶対パス。ファイル名が空の場合は、get_stylesheet_directory() で取得する絶対パス。

従来は、親テーマと子テーマのディレクトリのパスを取得する場合、以下のように使い分ける必要がありました。

get_theme_file_path() を使うと、指定されたファイルが子テーマに存在すればそのパスを、子テーマにファイルが存在しなければ親テーマの指定されたファイルのパスを返します。

※同名のファイルが親テーマと子テーマに存在すると子テーマ側のファイルが優先されます。

必ず親テーマのファイルのパスを取得したい場合は、get_parent_theme_file_path() を使います。

パラメータを指定しない場合の出力例(テーマ名が「my-theme」の場合)
<?php 
  $path = get_theme_file_path(); 
  echo $path;
?>

//ローカル環境の場合の出力例(末尾にスラッシュなし)
C:\xampp\htdocs\wp5/wp-content/themes/my-theme

以下はテーマフォルダ内の php フォルダにある my_file.php ファイルをインクルードする例です。

<?php include( get_theme_file_path( '/php/my_file.php' )); ?>

以下は、wp_enqueue_style() を使っての CSS の読み込みで、パラメータの指定で filemtime() の引数(6行目)にパスを指定する例です。

function my_styles() {	
  wp_enqueue_style(
    'my-style',
    get_theme_file_uri( '/css/style.css' ),
    array(),
    filemtime( get_theme_file_path( '/css/style.css' ) )
  );
}
add_action('wp_enqueue_scripts', 'my_styles');

get_parent_theme_file_uri

子テーマから親テーマのファイルの URL を取得したい場合に使用します。末尾にスラッシュは含まれません。WordPress 4.7 から利用可能。

get_parent_theme_file_uri( $file )

パラメータ
$file (string):(オプション)ファイル名。初期値:""(空)
戻り値
親テーマ内にあるファイルの URL を返します。ファイル名が空の場合は、get_template_directory_uri() で取得する URL。

親テーマの画像(/images/common/01.png)を表示する例

<img src="<?php echo get_parent_theme_file_uri('/images/common/01.png'); ?>" alt="...">

この関数は以下のように「wp-includes/link-template.php」で定義されています。

function get_parent_theme_file_uri( $file = '' ) {
    $file = ltrim( $file, '/' );
 
    if ( empty( $file ) ) {
        $url = get_template_directory_uri(); //ファイル名が空の場合
    } else {
        $url = get_template_directory_uri() . '/' . $file;
    }
 
    /**
     * Filters the URL to a file in the parent theme.
     *
     * @since 4.7.0
     *
     * @param string $url  The file URL.
     * @param string $file The requested file to search for.
     */
    return apply_filters( 'parent_theme_file_uri', $url, $file );
}

get_parent_theme_file_path

子テーマから親テーマのファイルの絶対パスを取得したい場合に使用します。末尾にスラッシュは含まれません。WordPress 4.7 から利用可能。

get_parent_theme_file_path( $file )

パラメータ
$file (string):(オプション)ファイル名。初期値:""(空)
戻り値
親テーマ内にあるファイルの絶対パス。ファイル名が空の場合は、get_template_directory() で取得する絶対パス。

親テーマ内の php フォルダにある my_file.php ファイルをインクルードする例

<?php include( get_parent_theme_file_path( '/php/my_file.php' )); ?>

この関数は以下のように「wp-includes/link-template.php」で定義されています。

function get_parent_theme_file_path( $file = '' ) {
    $file = ltrim( $file, '/' );
 
    if ( empty( $file ) ) {
        $path = get_template_directory();  //ファイル名が空の場合
    } else {
        $path = get_template_directory() . '/' . $file;
    }
 
    /**
     * Filters the path to a file in the parent theme.
     *
     * @since 4.7.0
     *
     * @param string $path The file path.
     * @param string $file The requested file to search for.
     */
    return apply_filters( 'parent_theme_file_path', $path, $file );
}

テーマのディレクトリ

get_template_directory_uri

有効化している(親)テーマのディレクトリの URI を取得します。末尾にスラッシュ( / )は含まれません。 (子テーマのディレクトリを取得するには get_stylesheet_directory_uri() を使用)

get_template_directory_uri()

パラメータ
なし
戻り値
テンプレートのあるディレクトリ URI
利用可能箇所
どこでも可能
出力例
http://example.com/wp-content/themes/my_theme 
画像を読み込む場合の例
<img src="<?php echo get_template_directory_uri(); ?>/images/01.jpg" alt="">
functions.php で JavaScript を読み込む場合の例
function my_template_scripts() {
  wp_enqueue_script( 'prettify_js', get_template_directory_uri() . '/prettify/prettify.js', array(), 'xxxxxxxx');
  ・・・
}
add_action( 'wp_enqueue_scripts', 'my_template_scripts' );

get_template_directory

末尾にスラッシュを付けずに、現在のテーマのディレクトリへの絶対サーバーパスを取得します。ファイルをインクルードする場合などに使用します。

子テーマが使用されている場合には、親テーマのディレクトリへの絶対パスが返されます。 子テーマのディレクトリへの絶対パスを取得するには get_stylesheet_directory() を使用します。

get_template_directory()

パラメータ
なし
戻り値
現在のテーマのディレクトリへの絶対パス
例: /home/user/public_html/wp-content/themes/my_theme
出力例(ローカル環境の場合)
C:\xampp\htdocs\sitename/wp-content/themes/my_theme 
PHP ファイルをインクルードする場合の例
<?php include( get_template_directory() . '/includes/myfile.php'); ?>

get_template_directory() の値は TEMPLATEPATH という定数で定義されているので以下のようにも記述できます(wp-include/default-constants.php に定義されています)。

定数 TEMPLATEPATH (現在の親テーマのディレクトリのパス)を使う場合の例
<?php include TEMPLATEPATH . '/includes/myfile.php'; ?>

get_stylesheet_directory_uri

テーマのスタイルシートディレクトリの URI を取得します。末尾にスラッシュ( / )は含まれません。スタイルシートや画像の参照などに使用するのに適しています。

子テーマを使用している場合は、親テーマのディレクトリの URI ではなく、子テーマのディレクトリの URIを取得します。親テーマのディレクトリを取得するには get_template_directory_uri() を使用します。

get_stylesheet_directory_uri()

パラメータ
なし
戻り値
スタイルシートディレクトリの URI
利用可能箇所
どこでも可能
出力例
http://example.com/wp-content/themes/my_theme 
画像を読み込む場合の例
<img src="<?php echo get_stylesheet_directory_uri(); ?>/images/sample.png" alt="">

get_stylesheet_directory

現在のテーマまたは子テーマのスタイルシートディレクトリの絶対サーバーパスを取得します(末尾にスラッシュを含みません)。

子テーマが有効な場合はそのディレクトリを返します。親テーマのディレクトリを取得するには get_template_directory() を使います。

get_stylesheet_directory()

パラメータ
なし
戻り値
スタイルシートディレクトリの絶対サーバーパス
例: /home/user/public_html/wp-content/themes/my_theme
出力例(ローカル環境の場合)
C:\xampp\htdocs\sitename/wp-content/themes/my_theme 
PHP ファイルをインクルードする場合の例
<?php include( get_stylesheet_directory() . '/includes/myfile.php'); ?>

get_stylesheet_directory() の値は STYLESHEETPATH という定数で定義されているので以下のようにも記述できます(wp-include/default-constants.php に定義されています)。

STYLESHEETPATH(現在のテーマまたは子テーマのスタイルシートのディレクトリへのパス)を使う場合の例
<?php include STYLESHEETPATH. '/includes/myfile.php'; ?>

また、include や require は関数ではなく特別な言語構造であるため、引数を囲む括弧 () は不要です(このページでも括弧を使って記述していたりしますが)。

get_stylesheet_uri

現状のテーマで使われているスタイルシートの URI(ファイル名を含む)を返す関数です。

get_stylesheet_uri()

パラメータ
なし
戻り値
現在のテーマのスタイルシート(style.css)の URI を返します。
利用可能箇所
どこでも
出力例
http://example.com/wp-content/themes/my_theme/style.css 
使用例 functions.php での style.css の読み込み
function my_template_styles() {
  wp_enqueue_style( 'my-template-style', get_stylesheet_uri() );
  ・・・
}
add_action( 'wp_enqueue_scripts', 'my_template_styles' );

get_theme_root_uri

テーマのルートディレクトリの URI を取得します。末尾にスラッシュ( / )は含まれません。

get_theme_root_uri( $stylesheet_or_template, $theme_roote )

パラメータ
  • $stylesheet_or_template(文字列):(オプション)テーマのスタイルシートまたはテンプレート名。初期値:false
  • $theme_roote(文字列):(オプション) テーマのルート。初期値:false
戻り値
テーマのルートディレクトリ URI
利用可能箇所
どこでも
<?php echo get_theme_root_uri(); ?>
          
http://localhost/wp-content/themes

以下は get_theme_root_uri() のソースです。

function get_theme_root_uri( $stylesheet_or_template = false, $theme_root = false ) {
    global $wp_theme_directories;
 
  if ( $stylesheet_or_template && ! $theme_root ) {
    $theme_root = get_raw_theme_root( $stylesheet_or_template );
  }

  if ( $stylesheet_or_template && $theme_root ) {
    if ( in_array( $theme_root, (array) $wp_theme_directories ) ) {
      // Absolute path. Make an educated guess. YMMV -- but note the filter below.
      if ( 0 === strpos( $theme_root, WP_CONTENT_DIR ) ) {
        $theme_root_uri = content_url( str_replace( WP_CONTENT_DIR, '', $theme_root ) );
      } elseif ( 0 === strpos( $theme_root, ABSPATH ) ) {
        $theme_root_uri = site_url( str_replace( ABSPATH, '', $theme_root ) );
      } elseif ( 0 === strpos( $theme_root, WP_PLUGIN_DIR ) || 0 === strpos( $theme_root, WPMU_PLUGIN_DIR ) ) {
        $theme_root_uri = plugins_url( basename( $theme_root ), $theme_root );
      } else {
        $theme_root_uri = $theme_root;
      }
    } else {
      $theme_root_uri = content_url( $theme_root );
    }
  } else {
      $theme_root_uri = content_url( 'themes' );
  }

  /**
   * Filters the URI for themes directory.
   * @since 1.5.0
   * @param string $theme_root_uri         The URI for themes directory.
   * @param string $siteurl                WordPress web address which is set in General Options.
   * @param string $stylesheet_or_template Stylesheet or template name of the theme.
   */
  return apply_filters( 'theme_root_uri', $theme_root_uri, get_option( 'siteurl' ), $stylesheet_or_template );
}

get_theme_root

テーマディレクトリの絶対パスを取得します。末尾にスラッシュ( / )は含まれません。

get_theme_root( $stylesheet_or_template )

パラメータ
$stylesheet_or_template(文字列):(オプション)テーマのスタイルシートまたはテンプレート名。初期値:false
戻り値
テーマディレクトリの絶対パス。末尾にスラッシュ( / )は含まれません。
利用可能箇所
どこでも
<?php echo get_theme_root(); ?>
          
C:\xampp\htdocs\sitename/wp-content/themes

以下は get_theme_root() のソースです。

function get_theme_root( $stylesheet_or_template = false ) {
  global $wp_theme_directories;

  if ( $stylesheet_or_template && $theme_root = get_raw_theme_root( $stylesheet_or_template ) ) {
    // Always prepend WP_CONTENT_DIR unless the root currently registered as a theme directory.
    // This gives relative theme roots the benefit of the doubt when things go haywire.
    if ( ! in_array( $theme_root, (array) $wp_theme_directories ) ) {
      $theme_root = WP_CONTENT_DIR . $theme_root;
    }
  } else {
    $theme_root = WP_CONTENT_DIR . '/themes';
  }

  /**
   * Filters the absolute path to the themes directory.
   * @since 1.5.0
   * @param string $theme_root Absolute path to themes directory.
   */
  return apply_filters( 'theme_root', $theme_root );
}

ブログ・サイトの URL

home_url

現在のブログのホーム URL を取得します。ホーム URL は、管理者画面「設定」-「一般」の「サイトアドレス (URL)」のことです。

home_url( $path, $scheme )

パラメータ
  • $path(文字列):(オプション) ホーム URL からの相対パス。初期値: なし
  • $scheme(文字列):(オプション) ホーム URL に使うスキーム。省略時は null(初期値)で自動判定。現在利用できるのは "http" と "https"、"rest" と "relative"(相対パス)。
戻り値
オプションの引数 $path の値を付加したホーム URL。※最後にスラッシュは付きません。
利用可能箇所
どこでも可能
現在のブログのホーム URL を出力する例。esc_url() でエスケープ処理しています。
<?php echo esc_url( home_url() ); ?>
//出力: http://www.example.com(最後のスラッシュがない)

<?php echo esc_url( home_url( '/' ) ); ?>
//出力: http://www.example.com/

<?php echo esc_url( home_url( '/sample' ) ); ?>
//出力: http://www.example.com/sample

<?php echo esc_url( home_url( 'sample' ) ); ?>
//出力: http://www.example.com/sample(上記と同じ結果)
<!-- $path を指定する場合、先頭に / を付けても、付けなくても自動的に調整される -->

<?php echo esc_url( home_url( '/sample/' ) ); ?>
//出力: http://www.example.com/sample/
<!-- 末尾の / は補完されないので指定する必要がある -->

<?php echo esc_url( home_url( '/', 'https' ) ); ?>
//出力: https://www.example.com/

<?php echo esc_url( home_url( '/sample', 'relative') ); ?>
//出力: /sample 

<?php echo esc_url( home_url( 'sample', 'relative') ); ?>
//出力: /sample (上記と同じ結果)

<?php echo esc_url( home_url( '/sample/', 'relative') ); ?>
//出力: /sample/
  
使用例
<a href="<?php echo esc_url( home_url( '/') ); ?>">Home</a>
<!--   出力結果    -->
<a href="http://www.example.com/">Home</a>

<a href="<?php echo esc_url( home_url( '/category/') ); ?>">Category</a>
<!--   出力結果     -->
<a href="http://www.example.com/category/">Category</a>

<a href="<?php echo esc_url( home_url( '/category/', 'relative') ); ?>">Category</a>
<!--   出力結果     -->
<a href="/category/">Category</a>

home_url() は、以下のように get_home_url() のラッパー関数として「wp-includes/link-template.php」で定義されています。

function home_url( $path = '', $scheme = null ) {
    return get_home_url( null, $path, $scheme );
}

get_home_url

任意の(指定した)ブログのホーム URL を取得します。ホーム URL は、管理者画面「設定」-「一般」の「サイトアドレス (URL)」のことです。

主にマルチサイトの場合に使用します。マルチサイトでない場合は、第一引数にサイト ID(ブログ ID)を指定する必要のない home_url() を使用します。

get_home_url( $blog_id, $path, $scheme )

パラメータ
  • $blog_id(整数):(オプション) ブログ ID。省略時は null(初期値)で現在のブログ。マルチサイトの場合、追加されたサイトには追加順にIDが付与され、WordPressのインストールと同時に作成されたメインのサイトのIDは「1」で、追加されたサイトは順に「2」以降のIDが付与されます。
  • $path(文字列):(オプション) ホーム URL への相対パス。初期値: なし
  • $scheme(文字列):(オプション) ホーム URL に使うスキーマ。省略時は null(初期値)で自動判定。現在利用できるのは 'http', 'https', 'relative' (相対パス), および 'rest'。

$path と $scheme の指定方法は home_url() と同じです。

戻り値
オプションの引数 $path の値を付加した($blog_id で指定した)ホーム URL。※最後にスラッシュは付きません。
利用可能箇所
どこでも可能
現在のブログのホーム URL を取得する
<?php $blog_home = get_home_url(); ?>
ブログ ID が2のホーム URL を最後にスラッシュを付けて取得する
<?php $blog2_home = get_home_url( 2, '/' ); ?>
現在のブログのホーム URL を最後にスラッシュを付けて取得する
<?php $blog_home = get_home_url( null, '/' ); ?>
使用例(ブログ ID が2のホーム URL へのリンク)
<a href="<?php echo esc_url( get_home_url( 2, '/' ) ); ?>">Blog2 (Home)</a>

get_home_url() は「wp-includes/link-template.php」で以下のように定義されています。

$blog_id が指定されていないか、マルチサイトでない場合は、現在のサイトで get_option( 'home' ) を使って値を取得しています。(6~7行目)

また、$path が指定されていれば、先頭に「/」を付けて($path の値の先頭に「/」があれば、ltrim() で取り除いて) URL を組み立てています。(23~24行目)

function get_home_url( $blog_id = null, $path = '', $scheme = null ) {
  global $pagenow;  //ページファイル名(グローバル変数)

  $orig_scheme = $scheme;

  if ( empty( $blog_id ) || !is_multisite() ) {
    $url = get_option( 'home' );
  } else {
    switch_to_blog( $blog_id );
    $url = get_option( 'home' );
    restore_current_blog();
  }

  if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) ) {
    if ( is_ssl() && ! is_admin() && 'wp-login.php' !== $pagenow )
      $scheme = 'https';
    else
      $scheme = parse_url( $url, PHP_URL_SCHEME );
  }

  $url = set_url_scheme( $url, $scheme );

  if ( $path && is_string( $path ) )
    $url .= '/' . ltrim( $path, '/' );

  /**
  * Filters the home URL.
  * @since 3.0.0
  * @param string $url The complete home URL including scheme and path.
  * @param string $path Path relative to the home URL. Blank string if no path is specified.
  * @param string|null $orig_scheme Scheme to give the home URL context. Accepts 'http', 'https','relative', 'rest', or null.
  * @param int|null $blog_id Site ID, or null for the current site.
  */
  return apply_filters( 'home_url', $url, $path, $orig_scheme, $blog_id );
}

site_url

現在のブログの WordPress コアファイルが置かれている位置(サイト URL)を返します。

「サイト URL」は、WordPress がインストールされている URL のことで、管理者ページの「設定」→「一般」の「WordPress アドレス (URL)」のことです。

site_url( $path, $scheme )

パラメータ
  • $path(文字列):(オプション) サイト URL(WordPress アドレス)からの相対パス。初期値: なし
  • $scheme(文字列):(オプション) サイト URL(WordPress アドレス)に使うスキーム。省略時は null(初期値)で自動判定。現在利用できるのは 'http'、'https'、'login'、'login_post'、'admin' と 'relative'(相対パス)。
戻り値
オプションの引数 $path の値を付加したスキームを含むサイト URL。※最後にスラッシュは付きません。
利用可能箇所
どこでも可能

返される値は、WordPress のインストールされているディレクトリの場所により、home_url() が返す値と同じ場合と異なる場合があります(参照:site_url と home_url の違い)。

また、この関数が返す値も home_url() 同様、最後にスラッシュ「/」が含まれません。

<?php 
  $url = site_url();
  echo $url;
?>

//出力結果(WordPress をドキュメントルートに設置している場合)
http://www.example.com

//出力結果(WordPress をドキュメントルートのサブディレクトリ「wp」に設置している場合)
http://www.example.com/wp

home_url() 同様、$path で指定する文字列の先頭のスラッシュ「/」はあってもなくても同じです。

<?php 
  $url = site_url('/secrets/', 'https');
  //または $url = site_url('secrets/', 'https');
  echo $url;
?>

//出力結果(WordPress をドキュメントルートに設置している場合)
https://www.example.com/secrets/

//出力結果(WordPress をドキュメントルートのサブディレクトリ「wp」に設置している場合)
https://www.example.com/wp/secrets/

site_url() は、以下のように get_site_url() のラッパー関数として「wp-includes/link-template.php」で定義されています。

function site_url( $path = '', $scheme = null ) {
    return get_site_url( null, $path, $scheme );
}
site_url と home_url の違い

site_url() と home_url() が返す値には以下のような違いがあります。

  • site_url()
    WordPress コアファイルが置かれているアドレス(URL)。
    言い換えると、WordPress がインストールされている位置(場所)のアドレス。
    管理画面「設定」→「一般設定」→「WordPress アドレス (URL)」の値
  • home_url()
    サイトのホームページ(を表示するため)のアドレス(URL)。
    管理画面「設定」→「一般設定」→「サイトアドレス (URL)」の値
    home_url() なのに「サイトアドレス」となっているので混乱しやすいかも知れません。

WordPress をドキュメントルートに設置している場合は、両方の値は同じになりますが、WordPress のインストール方法によっては異ります。

WordPress コアファイルをドキュメントルートに設置している場合(両方の値は同じ)

  • site_url(WordPress アドレス): http://example.com
  • home_url(サイトアドレス): http://example.com

WordPress コアファイルを「http://example.com/wp/」にインストールして、閲覧者には「http://example.com/」で表示する場合は、以下のように異なります。

  • site_url(WordPress アドレス): http://example.com/wp
  • home_url(サイトアドレス): http://example.com

WordPress コアファイルを「http://example.com/wp/」にインストールして、閲覧者にも「http://example.com/wp/」で表示する場合は、以下のように同じになります。

  • site_url(WordPress アドレス): http://example.com/wp
  • home_url(サイトアドレス): http://example.com/wp

それぞれの値が同じになる場合もありますが、それぞれ意味の異なる値です。

Codex(日本語):WordPress を専用ディレクトリに配置する

get_site_url

任意の(指定した)ブログの WordPress コアファイルが置かれている位置(サイト URL)を返します。

「サイト URL」は、WordPress がインストールされている URL のことで、管理者ページの「設定」→「一般」の「WordPress アドレス (URL)」のことです。

主にマルチサイトの場合に使用し、マルチサイトでない場合は、第一引数にサイト ID(ブログ ID)を指定する必要のない site_url() を使用します。

get_site_url( $blog_id, $path, $scheme )

パラメータ
  • $blog_id(整数):(オプション) ブログ ID。省略時(初期値)は現在のブログ。
  • $path(文字列):(オプション) サイト URL(WordPress アドレス)への相対パス。初期値: なし
  • $scheme(文字列):(オプション) サイト URL(WordPress アドレス)に使うスキーム。省略時は null(初期値)で自動判定。現在利用できるのは 'http'、'https'、'login'、'login_post'、'admin' と 'relative'(相対パス)。
戻り値
オプションの引数 $path の値を付加したスキームを含むサイト URL。※最後にスラッシュは付きません。
利用可能箇所
どこでも可能
現在のブログのサイト URL を取得する
<?php $url = get_site_url();  ?>
//取得する値の例(最後にスラッシュ「/」が含まれません)

//WordPress をドキュメントルートに設置している場合
http://www.example.com

//WordPress をドキュメントルートのサブディレクトリ「wp」に設置している場合
http://www.example.com/wp
ブログ ID が2のサイト URL を最後にスラッシュを付けて取得する
<?php $blog2_site = get_site_url( 2, '/' ); ?>
現在のブログのサイト URL を最後にスラッシュを付けて取得する
<?php $url = get_site_url(null, '/');  ?>

ディレクトリの URL

以下のような WordPress のディレクトリの URL を取得する関数があります。

関数 ディレクトリ echo 結果例(パラメータ指定なしで実行)
includes_url() wp-includes http://example.com/wp-includes/
admin_url() wp-admin http://example.com/wp-admin/
content_url() wp-content http://example.com/wp-content
plugins_url() plugins http://example.com/wp-content/plugins
wp_upload_dir() uploads 戻り値は配列(echo できない ※)

※ wp_upload_dir() の戻り値は以下のような配列になっています(var_dump() の実行結果の例)。

array(6) {
  ["path"]=>
  string(46) "C:\path\to\wordpress/wp-content/uploads/2019/01"
  ["url"]=>
  string(47) "http://example.com/wp-content/uploads/2019/01"
  ["subdir"]=>
  string(8) "/2019/01"
  ["basedir"]=>  //ベースディレクトリ
  string(38) "C:\path\to\wordpress/wp-content/uploads"
  ["baseurl"]=>  //ベース URL
  string(39) "http://example.com/wp-content/uploads"
  ["error"]=>
  bool(false)
}

以下はベース URL を取得する例です。

<?php 
$uploads_baseurl = wp_upload_dir()['baseurl']; 
echo $uploads_baseurl;
?>

http://example.com/wp-content/uploads

現在のページの URL

現在のページの URL は get_pagenum_link()get_query_var() を使って以下のようにして取得できます。

<?php $current_url =  get_pagenum_link(get_query_var('paged')); ?>
          
//静的フロントページや個別ページの場合は paged ではなく page を使用        
<?php $current_url =  get_pagenum_link(get_query_var('page')); ?>