函数文档

get_dashboard_url()

💡 云策文档标注

概述

get_dashboard_url() 函数用于获取用户仪表盘的 URL,根据用户权限和站点归属动态返回相应地址。支持多站点环境下的不同场景处理。

关键要点

  • 函数返回用户仪表盘的 URL,可附加可选路径和指定协议。
  • 参数包括可选的用户 ID、路径和协议,默认使用当前用户和 'admin' 协议。
  • 在多站点环境中,根据用户是否属于当前站点或拥有管理权限,返回站点仪表盘或用户全局仪表盘。
  • 提供 'user_dashboard_url' 过滤器,允许自定义 URL 生成。

代码示例

function get_dashboard_url( $user_id = 0, $path = '', $scheme = 'admin' ) {
	$user_id = $user_id ? (int) $user_id : get_current_user_id();

	$blogs = get_blogs_of_user( $user_id );

	if ( is_multisite() && ! user_can( $user_id, 'manage_network' ) && empty( $blogs ) ) {
		$url = user_admin_url( $path, $scheme );
	} elseif ( ! is_multisite() ) {
		$url = admin_url( $path, $scheme );
	} else {
		$current_blog = get_current_blog_id();

		if ( $current_blog && ( user_can( $user_id, 'manage_network' ) || in_array( $current_blog, array_keys( $blogs ), true ) ) ) {
			$url = admin_url( $path, $scheme );
		} else {
			$active = get_active_blog_for_user( $user_id );
			if ( $active ) {
				$url = get_admin_url( $active->blog_id, $path, $scheme );
			} else {
				$url = user_admin_url( $path, $scheme );
			}
		}
	}

	/**
	 * Filters the dashboard URL for a user.
	 *
	 * @since 3.1.0
	 *
	 * @param string $url     The complete URL including scheme and path.
	 * @param int    $user_id The user ID.
	 * @param string $path    Path relative to the URL. Blank string if no path is specified.
	 * @param string $scheme  Scheme to give the URL context. Accepts 'http', 'https', 'login',
	 *                        'login_post', 'admin', 'relative' or null.
	 */
	return apply_filters( 'user_dashboard_url', $url, $user_id, $path, $scheme );
}

注意事项

  • 在多站点设置中,函数逻辑复杂,需仔细测试用户权限和站点归属。
  • 使用 'user_dashboard_url' 过滤器时,确保回调函数正确处理所有参数。
  • 路径参数应仅使用站点和用户管理员都已知的路径,以避免安全风险。

📄 原文内容

Retrieves the URL to the user’s dashboard.

Description

If a user does not belong to any site, the global user dashboard is used. If the user belongs to the current site, the dashboard for the current site is returned. If the user cannot edit the current site, the dashboard to the user’s primary site is returned.

Parameters

$user_idintoptional
User ID. Defaults to current user.
$pathstringoptional
Optional path relative to the dashboard. Use only paths known to both site and user admins. 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 Dashboard URL link with optional path appended.

Source

function get_dashboard_url( $user_id = 0, $path = '', $scheme = 'admin' ) {
	$user_id = $user_id ? (int) $user_id : get_current_user_id();

	$blogs = get_blogs_of_user( $user_id );

	if ( is_multisite() && ! user_can( $user_id, 'manage_network' ) && empty( $blogs ) ) {
		$url = user_admin_url( $path, $scheme );
	} elseif ( ! is_multisite() ) {
		$url = admin_url( $path, $scheme );
	} else {
		$current_blog = get_current_blog_id();

		if ( $current_blog && ( user_can( $user_id, 'manage_network' ) || in_array( $current_blog, array_keys( $blogs ), true ) ) ) {
			$url = admin_url( $path, $scheme );
		} else {
			$active = get_active_blog_for_user( $user_id );
			if ( $active ) {
				$url = get_admin_url( $active->blog_id, $path, $scheme );
			} else {
				$url = user_admin_url( $path, $scheme );
			}
		}
	}

	/**
	 * Filters the dashboard URL for a user.
	 *
	 * @since 3.1.0
	 *
	 * @param string $url     The complete URL including scheme and path.
	 * @param int    $user_id The user ID.
	 * @param string $path    Path relative to the URL. Blank string if no path is specified.
	 * @param string $scheme  Scheme to give the URL context. Accepts 'http', 'https', 'login',
	 *                        'login_post', 'admin', 'relative' or null.
	 */
	return apply_filters( 'user_dashboard_url', $url, $user_id, $path, $scheme );
}

Hooks

apply_filters( ‘user_dashboard_url’, string $url, int $user_id, string $path, string $scheme )

Filters the dashboard URL for a user.

Changelog

Version Description
3.1.0 Introduced.