SYNCER

SYNCER

get_home_url() - 指定したブログのホームのURLを取得する

公開日:

get_home_url()は、指定したIDのブログの、ホームのURLアドレスを取得する関数です。第1引数で追加のパスを指定できます。複数のブログを運用している時に有用です。

構文

string get_home_url( int $blog_id = null, string $path = '', string $scheme = null )

パラメータ

$blog_id

初期値: null

ブログのID。省略、またはnullを指定した場合は、現在のブログのURLを取得する。

$path

初期値: ''

追加するパス。スラッグ(/)で始めること。

$scheme

初期値: null

通常、スキーム(httphttps)は環境によって自動判定されるが、ここで指定すると強制的にその通りにできる。relative を指定すれば、サーバー相対パスを取得できる。

"http"
スキームをhttpにする。
"https"
スキームをhttpsにする。
"relative"
URLではなく、サーバー相対パスにする。
null
http、またはhttpsを自動判定する。

返り値

string

絶対パスのURLアドレスを取得する。第3引数にrelativeを指定した場合だけ、サーバー相対パスを取得する。

関数

この関数内で利用している、別のWordPress関数です。

  • apply_filters() - フィルターイベントを発火する。
  • get_option()
  • restore_current_blog()
  • set_url_scheme()
  • switch_to_blog()

フック

関数内で呼び出されるイベントです。

フィルター

home_url

サンプルコード

ホームのURLを取得する

ブログのホームのURLアドレスを取得します。第1引数でブログIDを指定しない場合は、現在のブログが対象になります。

php

<?php
	// 実行
	$result = get_home_url() ;

	// 結果
	print_r( $result ) ;

結果 (出力内容) - PHP7.0.13

https://wp.syncer.jp

パスを追加する

第1引数に追加のパスを指定できます。スラッシュ(/)から指定して下さい。

php

<?php
	// 実行
	$result = get_home_url( null, "/category/animal/" ) ;

	// 結果
	print_r( $result ) ;

結果 (出力内容) - PHP7.0.13

https://wp.syncer.jp/category/animal/

スキームを指定する

第2引数でスキームを強制できます。「SSLサイトだけど事情があって、httpにしたい」などといった場合に有用です。

php

<?php
	// 実行
	$result = get_home_url( null, "/category/animal/", "http" ) ;

	// 結果
	print_r( $result ) ;

結果 (出力内容) - PHP7.0.13

http://wp.syncer.jp/category/animal/

相対パスを取得する

第2引数にスキームではなく"relative"を指定した場合、絶対パスではなくサーバー相対パスを取得できます。ホームのサーバー相対パスを指定する際はスラッシュ(/)だけを指定しましょう。

php

<?php
	// 実行
	$result = get_home_url( null, "/category/animal/", "relative" ) ;

	// 結果
	print_r( $result ) ;

結果 (出力内容) - PHP7.0.13

/category/animal/

ソースコード

wp-includes/link-template.php

/**
 * Retrieves the URL for a given site where the front end is accessible.
 *
 * Returns the 'home' option with the appropriate protocol. The protocol will be 'https'
 * if is_ssl() evaluates to true; otherwise, it will be the same as the 'home' option.
 * If `$scheme` is 'http' or 'https', is_ssl() is overridden.
 *
 * @since 3.0.0
 *
 * @global string $pagenow
 *
 * @param  int         $blog_id Optional. Site ID. Default null (current site).
 * @param  string      $path    Optional. Path relative to the home URL. Default empty.
 * @param  string|null $scheme  Optional. Scheme to give the home URL context. Accepts
 *                              'http', 'https', 'relative', 'rest', or null. Default null.
 * @return string Home URL link with optional path appended.
 */
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 );
}

参考リンク