函数文档

network_admin_url()

💡 云策文档标注

概述

network_admin_url() 函数用于获取当前站点网络管理区域的 URL,支持多站点环境。如果站点未启用多站点,则回退到 admin_url()。

关键要点

  • 函数返回网络管理区域的 URL,可附加可选路径。
  • 参数 $path 为可选相对路径,默认为空字符串。
  • 参数 $scheme 控制协议,默认为 'admin',自动根据 force_ssl_admin() 和 is_ssl() 决定;可指定 'http' 或 'https' 强制覆盖。
  • 在非多站点环境中,自动调用 admin_url() 替代。
  • 提供 'network_admin_url' 过滤器,允许自定义 URL。

代码示例

$url = network_admin_url();
echo $url;
// 输出示例: http://www.example.com/wp-admin/network/

$url = network_admin_url( 'user-new.php', 'https' );
echo $url;
// 输出示例: https://www.example.com/wp-admin/network/user-new.php

📄 原文内容

Retrieves the URL to the admin area for the network.

Parameters

$pathstringoptional
Optional path relative to the admin URL. Default empty.
$schemestringoptional
The scheme to use. Default is 'admin', which obeys force_ssl_admin() and is_ssl() . 'http' or 'https' can be passed to force those schemes.

Return

string Admin URL link with optional path appended.

More Information

  • The network_admin_url template tag retrieves the URL to the Network Admin area for the current site with the appropriate protocol, “https” if is_ssl() and “http” otherwise. If $scheme is “http” or “https”, is_ssl() is overridden.
  • If the site is not setup as multisite, admin_url() will be used instead.

Source

function network_admin_url( $path = '', $scheme = 'admin' ) {
	if ( ! is_multisite() ) {
		return admin_url( $path, $scheme );
	}

	$url = network_site_url( 'wp-admin/network/', $scheme );

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

	/**
	 * Filters the network admin URL.
	 *
	 * @since 3.0.0
	 * @since 5.8.0 The `$scheme` parameter was added.
	 *
	 * @param string      $url    The complete network admin URL including scheme and path.
	 * @param string      $path   Path relative to the network admin URL. Blank string if
	 *                            no path is specified.
	 * @param string|null $scheme The scheme to use. Accepts 'http', 'https',
	 *                            'admin', or null. Default is 'admin', which obeys force_ssl_admin() and is_ssl().
	 */
	return apply_filters( 'network_admin_url', $url, $path, $scheme );
}

Hooks

apply_filters( ‘network_admin_url’, string $url, string $path, string|null $scheme )

Filters the network admin URL.

Changelog

Version Description
3.0.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Basic Example

    $url = network_admin_url();
    echo $url;

    Output: http://www.example.com/wp-admin/network/

    (protocol will be https when appropriate)

    // generate url path to Users -> Add New page in the admin and force https
    $url = network_admin_url( 'user-new.php', 'https' );
    echo $url;

    Output: https://www.example.com/wp-admin/network/user-new.php