user_admin_url()
云策文档标注
概述
user_admin_url() 函数用于获取当前用户的 WordPress 管理区域 URL,支持添加相对路径和指定协议方案。
关键要点
- 函数返回当前用户的管理区域 URL,基于 network_site_url() 构建,默认路径为 'wp-admin/user/'。
- 接受两个可选参数:$path 用于追加相对路径,$scheme 用于指定协议(如 'admin'、'http'、'https')。
- 通过 apply_filters('user_admin_url', ...) 提供钩子,允许开发者过滤生成的 URL。
- 与 self_admin_url()、get_dashboard_url() 等函数相关,常用于用户仪表板或编辑个人资料等场景。
代码示例
function user_admin_url( $path = '', $scheme = 'admin' ) {
$url = network_site_url( 'wp-admin/user/', $scheme );
if ( $path && is_string( $path ) ) {
$url .= ltrim( $path, '/' );
}
return apply_filters( 'user_admin_url', $url, $path, $scheme );
}注意事项
- 默认 $scheme 为 'admin',它会根据 force_ssl_admin() 和 is_ssl() 自动选择 HTTP 或 HTTPS。
- 函数自 WordPress 3.0.0 版本引入,在 5.8.0 版本中增加了 $scheme 参数。
原文内容
Retrieves the URL to the admin area for the current user.
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.
Source
function user_admin_url( $path = '', $scheme = 'admin' ) {
$url = network_site_url( 'wp-admin/user/', $scheme );
if ( $path && is_string( $path ) ) {
$url .= ltrim( $path, '/' );
}
/**
* Filters the user admin URL for the current user.
*
* @since 3.1.0
* @since 5.8.0 The `$scheme` parameter was added.
*
* @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|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( 'user_admin_url', $url, $path, $scheme );
}
Hooks
- apply_filters( ‘user_admin_url’, string $url, string $path, string|null $scheme )
-
Filters the user admin URL for the current user.
Changelog
| Version | Description |
|---|---|
| 3.0.0 | Introduced. |