函数文档

get_admin_url()

💡 云策文档标注

概述

get_admin_url() 函数用于获取指定站点的管理区域 URL,支持自定义路径和协议方案。它基于 get_site_url() 构建,并可通过 'admin_url' 过滤器进行修改。

关键要点

  • 函数返回管理区域的 URL,可附加相对路径。
  • 参数包括可选的站点 ID、路径和协议方案,默认使用当前站点和 'admin' 方案。
  • 使用 'admin_url' 过滤器可以自定义生成的 URL。
  • 相关函数包括 get_site_url()、admin_url() 和 network_admin_url()。

代码示例

function get_admin_url( $blog_id = null, $path = '', $scheme = 'admin' ) {
	$url = get_site_url( $blog_id, 'wp-admin/', $scheme );

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

	return apply_filters( 'admin_url', $url, $path, $blog_id, $scheme );
}

注意事项

  • 协议方案 'admin' 会根据 force_ssl_admin() 和 is_ssl() 自动选择 HTTP 或 HTTPS。
  • 获取网络管理 URL 应使用 network_admin_url() 函数。

📄 原文内容

Retrieves the URL to the admin area for a given site.

Parameters

$blog_idint|nulloptional
Site ID. Default null (current site).

Default:null

$pathstringoptional
Path relative to the admin URL. Default empty.
$schemestringoptional
The scheme to use. Accepts 'http' or 'https', to force those schemes. Default 'admin', which obeys force_ssl_admin() and is_ssl() .

Return

string Admin URL link with optional path appended.

Source

function get_admin_url( $blog_id = null, $path = '', $scheme = 'admin' ) {
	$url = get_site_url( $blog_id, 'wp-admin/', $scheme );

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

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

Hooks

apply_filters( ‘admin_url’, string $url, string $path, int|null $blog_id, string|null $scheme )

Filters the admin area URL.

Changelog

Version Description
3.0.0 Introduced.

User Contributed Notes