函数文档

self_admin_url()

💡 云策文档标注

概述

self_admin_url() 函数用于根据当前上下文(如网络管理、用户管理或站点管理)动态获取管理区域的 URL。它接受可选参数以指定相对路径和协议方案,并返回完整的 URL 字符串。

关键要点

  • 函数根据 is_network_admin() 和 is_user_admin() 判断上下文,分别调用 network_admin_url()、user_admin_url() 或 admin_url() 生成 URL。
  • 参数 $path 用于附加相对路径,$scheme 指定协议(默认为 'admin',遵循 force_ssl_admin() 和 is_ssl() 设置)。
  • 通过 apply_filters('self_admin_url', $url, $path, $scheme) 钩子允许过滤返回的 URL。
  • 函数自 WordPress 3.1.0 版本引入,常用于插件和主题开发中生成管理链接。

代码示例

function self_admin_url( $path = '', $scheme = 'admin' ) {
    if ( is_network_admin() ) {
        $url = network_admin_url( $path, $scheme );
    } elseif ( is_user_admin() ) {
        $url = user_admin_url( $path, $scheme );
    } else {
        $url = admin_url( $path, $scheme );
    }

    /**
     * Filters the admin URL for the current site or network depending on context.
     *
     * @since 4.9.0
     *
     * @param string $url    The complete URL including scheme and path.
     * @param string $path   Path relative to the URL. Blank string if no path is specified.
     * @param string $scheme The scheme to use.
     */
    return apply_filters( 'self_admin_url', $url, $path, $scheme );
}

📄 原文内容

Retrieves the URL to the admin area for either the current site or the network depending on context.

Parameters

$pathstringoptional
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.

Source

function self_admin_url( $path = '', $scheme = 'admin' ) {
	if ( is_network_admin() ) {
		$url = network_admin_url( $path, $scheme );
	} elseif ( is_user_admin() ) {
		$url = user_admin_url( $path, $scheme );
	} else {
		$url = admin_url( $path, $scheme );
	}

	/**
	 * Filters the admin URL for the current site or network depending on context.
	 *
	 * @since 4.9.0
	 *
	 * @param string $url    The complete URL including scheme and path.
	 * @param string $path   Path relative to the URL. Blank string if no path is specified.
	 * @param string $scheme The scheme to use.
	 */
	return apply_filters( 'self_admin_url', $url, $path, $scheme );
}

Hooks

apply_filters( ‘self_admin_url’, string $url, string $path, string $scheme )

Filters the admin URL for the current site or network depending on context.

Changelog

Version Description
3.1.0 Introduced.