函数文档

network_home_url()

💡 云策文档标注

概述

network_home_url() 函数用于检索当前网络的主页 URL,支持自定义路径和协议方案。在非多站点环境中,它会回退到 home_url() 函数。

关键要点

  • 函数返回当前网络的主页 URL,可根据 is_ssl() 自动选择协议(https 或 http),或通过 $scheme 参数覆盖。
  • 参数 $path 用于附加相对路径,$scheme 接受 'http'、'https' 或 'relative',默认为 null。
  • 内部逻辑包括检查多站点状态、处理协议方案、构建 URL 并应用 network_home_url 过滤器。
  • 相关函数包括 get_network()、is_ssl()、set_url_scheme() 等,用于支持其功能。

代码示例

$url = network_home_url();
echo $url;
// 输出: http://www.example.com

注意事项

  • 注意 URL 可能不包含尾部斜杠,但实际输出可能因网络配置而异,建议不依赖于此特性。
  • 在非多站点安装中,此函数会调用 home_url(),确保兼容性。

📄 原文内容

Retrieves the home URL for the current network.

Description

Returns the home URL with the appropriate protocol, ‘https’ is_ssl() and ‘http’ otherwise. If $scheme is ‘http’ or ‘https’, is_ssl() is overridden.

Parameters

$pathstringoptional
Path relative to the home URL. Default empty.
$schemestring|nulloptional
Scheme to give the home URL context. Accepts 'http', 'https', or 'relative'.

Default:null

Return

string Home URL link with optional path appended.

Source

function network_home_url( $path = '', $scheme = null ) {
	if ( ! is_multisite() ) {
		return home_url( $path, $scheme );
	}

	$current_network = get_network();
	$orig_scheme     = $scheme;

	if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ), true ) ) {
		$scheme = is_ssl() ? 'https' : 'http';
	}

	if ( 'relative' === $scheme ) {
		$url = $current_network->path;
	} else {
		$url = set_url_scheme( 'http://' . $current_network->domain . $current_network->path, $scheme );
	}

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

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

Hooks

apply_filters( ‘network_home_url’, string $url, string $path, string|null $orig_scheme )

Filters the network home URL.

Changelog

Version Description
3.0.0 Introduced.

User Contributed Notes