 WordPress のURL URI パスを取得・出力する関数
 WordPress のURL URI パスを取得・出力する関数
      HTML ファイルの場合、通常は画像や他のファイルへのパスはそのファイルが置いてある場所から相対的な場所(位置)を指定しますが、WordPress では指定の方法が異なります。
静的な HTML ファイルの場合はファイルの位置はその構造に一致しているので相対的なパスで指定できますが、WordPress の場合は URL の構造とファイルの物理的な構造が一致しているわけではないので、 URL やパスを取得するにはテンプレートタグを使います。
関連ページ:「WordPress のフォルダ構成/テーマのディレクトリへのパス」
更新日:2024年10月20日
作成日:2019年01月18日
テーマ内ファイル
現在有効になっているテーマのフォルダ内にあるファイルの URI(URL) や パスを取得する関数。
get_theme_file_uri
現在有効なテーマフォルダ内にあるファイルの URL を取得します。WordPress 4.7 から利用可能です。
get_theme_file_uri( $file )
- パラメータ
- $file (string):(オプション)検索するファイル。先頭にスラッシュがあってもなくてもOK。
 初期値:""(空)
- 戻り値
- テーマ内にあるファイルの URL。引数($file)が空の場合は、get_stylesheet_directory_uri() で取得する URL。
<?php $uri = get_theme_file_uri(); echo $uri; ?> //テーマ名が「my-theme」の場合出力例(末尾にスラッシュなし) http://example.com/wp-content/themes/my-theme
従来は、親テーマと子テーマのディレクトリの URL を取得する場合、以下のように使い分ける必要がありました。
- 親テーマの URL: get_template_directory_uri()
- 子テーマの URL: get_stylesheet_directory_uri()
get_theme_file_uri() を使うと、指定されたファイルが子テーマに存在すればその URL を、子テーマにファイルが存在しなければ親テーマの指定されたファイルの URL を返します。
※同名のファイルが親テーマと子テーマに存在すると子テーマ側のファイルが優先されます。
必ず親テーマのファイルの URL を取得したい場合は、get_parent_theme_file_uri() を使います。
この関数は以下のように「wp-includes/link-template.php」で定義されています。
- 
              引数の $file の先頭にスラッシュがあれば、一旦 ltrim() で削除。(2行目) 
- 
              $file が空の場合は、get_stylesheet_directory_uri() で取得される値が返されます。(4~5行目) 
- 
              file_exists で子テーマのディレクトリ内に指定されたファイルが存在するかを調べ、あればそのファイルの URL を設定します。その際、スラッシュを $file の前に付けます。(6~7行目) 
- 
              子テーマのディレクトリ内に指定されたファイルが存在しなければ、親テーマのディレクトリにファイルを指定して URL を設定します(スラッシュを $file の前に付けます)。(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):(オプション)検索するファイル。先頭にスラッシュがあってもなくてもOK。
 初期値:""(空)
- 戻り値
- テーマ内にあるファイルの絶対パス。ファイル名が空の場合は、get_stylesheet_directory() で取得する絶対パス。
従来は、親テーマと子テーマのディレクトリのパスを取得する場合、以下のように使い分ける必要がありました。
- 親テーマのパス: get_template_directory()
- 子テーマのパス: get_stylesheet_directory()
get_theme_file_path() を使うと、指定されたファイルが子テーマに存在すればそのパスを、子テーマにファイルが存在しなければ親テーマの指定されたファイルのパスを返します。
※同名のファイルが親テーマと子テーマに存在すると子テーマ側のファイルが優先されます。
必ず親テーマのファイルのパスを取得したい場合は、get_parent_theme_file_path() を使います。
<?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):(オプション)検索するファイル。先頭にスラッシュがあってもなくてもOK。
 初期値:""(空)
- 戻り値
- 親テーマ内にあるファイルの 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):(オプション)検索するファイル。先頭にスラッシュがあってもなくてもOK。
 初期値:""(空)
- 戻り値
- 親テーマ内にあるファイルの絶対パス。ファイル名が空の場合は、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="">
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 include( get_template_directory() . '/includes/myfile.php'); ?>
get_template_directory() の値は TEMPLATEPATH という定数で定義されているので以下のようにも記述できます(wp-include/default-constants.php に定義されています)。
<?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 include( get_stylesheet_directory() . '/includes/myfile.php'); ?>
get_stylesheet_directory() の値は STYLESHEETPATH という定数で定義されているので以下のようにも記述できます(wp-include/default-constants.php に定義されています)。
<?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
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。※最後にスラッシュは付きません。
- 利用可能箇所
- どこでも可能
<?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。※最後にスラッシュは付きません。
- 利用可能箇所
- どこでも可能
<?php $blog_home = get_home_url(); ?>
<?php $blog2_home = get_home_url( 2, '/' ); ?>
<?php $blog_home = get_home_url( null, '/' ); ?>
<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。※最後にスラッシュは付きません。
- 利用可能箇所
- どこでも可能
<?php $url = get_site_url(); ?> //取得する値の例(最後にスラッシュ「/」が含まれません) //WordPress をドキュメントルートに設置している場合 http://www.example.com //WordPress をドキュメントルートのサブディレクトリ「wp」に設置している場合 http://www.example.com/wp
<?php $blog2_site = get_site_url( 2, '/' ); ?>
<?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')); ?>
        get_pagenum_link
指定されたページ番号のリンクを取得します。
get_pagenum_link( $pagenum, $escape )
- パラメータ
- 
                - $pagenum(整数):(オプション)ページ番号。初期値:1
- $escape(真偽値):(オプション)取得する URL のエスケープ処理に esc_url() を使う場合は true(初期値)。false を指定すると esc_url_raw() を使用。
 
- 戻り値
- 指定されたページ番号の URL
例えばトップページでパラメータなしで以下のように記述すると、トップページの URL が取得され表示されます。
<?php echo get_pagenum_link(); ?> <!-- 出力例 http://example.com/ -->
トップページの投稿数が1ページに表示する最大投稿数よりも多くページが分割されている場合に、以下を記述すると分割されたページ番号が付いた URL が取得され表示されます。
<?php echo get_pagenum_link(get_query_var('paged')); ?>
<!-- 出力例 http://example.com/page/2/ (2ページ目の場合) -->
            get_query_var('paged') はアーカイブページでページ分割されている場合そのページ番号を取得します。
news と言うカテゴリーのアーカイブページの3ページ目では、以下のような URL が取得されます。
<?php echo get_pagenum_link(get_query_var('paged')); ?>
<!-- 出力例 http://example.com/category/news/page/3/ (3ページ目の場合) -->
            つまり、以下の記述で現在のページの URL を取得することができます。
<?php $current_url =  get_pagenum_link(get_query_var('paged')); ?>
            静的フロントページや個別ページの場合は、paged ではなく page を使うので以下のようになります。
<?php $current_url =  get_pagenum_link(get_query_var('page')); ?>
            以下はアーカイブページでページが分割されている場合に、ページ番号の付いた URL のリンクを表示する例です。
実際にはページの種類やパーマリンク設定などその他色々なことを考慮しなければならないので実用的なものではありませんが、最も単純なページネーションのようなものです。
静的フロントページや個別ページの場合は、paged ではなく page を使います。
<?php
global $wp_query;
$total_pages = $wp_query-> max_num_pages;  //総ページ数
$current_page = get_query_var('paged') ? get_query_var('paged'): 1;
for ($i = 1; $i <= $total_pages; $i++) {
  if ($i === $current_page) {
    echo '<span>'. $i . '</span>';
  }else {
    echo '<a href="' . get_pagenum_link($i) . '" >'. $i .'</a>';
  }
}
?>
            関連項目:paginate_links()
以下は get_pagenum_link() のソースです。
function get_pagenum_link( $pagenum = 1, $escape = true ) {
  global $wp_rewrite;
  $pagenum = (int) $pagenum;
  $request = remove_query_arg( 'paged' );
  $home_root = parse_url( home_url() );
  $home_root = ( isset( $home_root['path'] ) ) ? $home_root['path'] : '';
  $home_root = preg_quote( $home_root, '|' );
  $request = preg_replace( '|^' . $home_root . '|i', '', $request );
  $request = preg_replace( '|^/+|', '', $request );
  if ( ! $wp_rewrite->using_permalinks() || is_admin() ) {
    $base = trailingslashit( get_bloginfo( 'url' ) );
    if ( $pagenum > 1 ) {
      $result = add_query_arg( 'paged', $pagenum, $base . $request );
    } else {
      $result = $base . $request;
    }
  } else {
    $qs_regex = '|\?.*?$|';
    preg_match( $qs_regex, $request, $qs_match );
    if ( ! empty( $qs_match[0] ) ) {
      $query_string = $qs_match[0];
      $request      = preg_replace( $qs_regex, '', $request );
    } else {
      $query_string = '';
    }
    $request = preg_replace( "|$wp_rewrite->pagination_base/\d+/?$|", '', $request );
    $request = preg_replace( '|^' . preg_quote( $wp_rewrite->index, '|' ) . '|i', '', $request );
    $request = ltrim( $request, '/' );
    $base = trailingslashit( get_bloginfo( 'url' ) );
    if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' != $request ) ) {
      $base .= $wp_rewrite->index . '/';
    }
    if ( $pagenum > 1 ) {
      $request = ( ( ! empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( $wp_rewrite->pagination_base . '/' . $pagenum, 'paged' );
    }
    $result = $base . $request . $query_string;
  }
  /**
   * Filters the page number link for the current request.
   * @since 2.5.0
   * @since 5.2.0 Added the `$pagenum` argument.
   * @param string $result  The page number link.
   * @param int    $pagenum The page number.
   */
  $result = apply_filters( 'get_pagenum_link', $result, $pagenum );
  if ( $escape ) {
    return esc_url( $result );
  } else {
    return esc_url_raw( $result );
  }
}
            インストールディレクトリのパス
WordPress をインストールしたディレクトリのフルパスを取得するには、get_home_path() や ABSPATH が使えます。但し、get_home_path() は wp-admin で定義されているので、デフォルトではフロントエンドでは使えません。
get_home_path
WordPress インストールディレクトリへの絶対ファイルシステムパス(フルパス)を取得します。
get_home_path()
- パラメータ
- なし
- 戻り値
- WordPress がインストールされているディレクトリへの絶対パス(文字列)
[注意]
この関数は wp-admin/includes/file.php で定義されているので、フロントエンドでは使えません。is_admin() で管理者ページ(/wp-admin/以下)であることを確認してから呼び出さないと以下のようなエラーになります。
Fatal error: Uncaught Error: Call to undefined function get_home_path() ...
または、require_once を使って wp-admin/includes/file.php を読み込みます。
以下は、/Applications/MAMP/htdocs/wp-sample/(Mac ローカル環境)に WordPress がインストールされている場合の例です。
require_once ABSPATH . 'wp-admin/includes/file.php'; echo get_home_path() ; // /Applications/MAMP/htdocs/wp-sample/ echo ABSPATH; // /Applications/MAMP/htdocs/wp-sample/
ABSPATH
定数 ABSPATH には WordPress インストールディレクトリへの絶対ファイルシステムパス(フルパス)が入っています。get_home_path() が返す値と同じです。
ABSPATH は wp-config.php で以下のように定義されています。
/** Absolute path to the WordPress directory. */
if ( ! defined( 'ABSPATH' ) ) {
  define( 'ABSPATH', __DIR__ . '/' );
}
        __DIR__ は PHP のマジック定数の1つで、そのファイルの存在するディレクトリが入ります。
ABSPATH は wp-config.php のあるディレクトリのフルパスの最後に / がついたもの、つまり、WordPress がインストールされているディレクトリへの絶対パスになります。