wp_admin_bar_my_account_menu()
云策文档标注
概述
wp_admin_bar_my_account_menu() 是一个 WordPress 函数,用于向管理工具栏添加“我的账户”子菜单项,包括用户信息和注销链接。它根据用户权限和站点配置动态生成菜单内容。
关键要点
- 函数接受一个 WP_Admin_Bar 实例作为必需参数,用于操作管理工具栏。
- 根据当前用户 ID 和权限,确定个人资料编辑 URL:有 'read' 权限时使用 get_edit_profile_url(),在 Multisite 中无权限时使用 get_dashboard_url(),否则为 false。
- 添加一个 'user-actions' 组到 'my-account' 父节点下,然后添加 'user-info' 节点(包含头像、显示名、用户名和编辑个人资料链接)和 'logout' 节点(注销链接)。
- 使用 get_avatar()、wp_get_current_user() 等辅助函数获取用户数据,并调用 WP_Admin_Bar::add_group() 和 WP_Admin_Bar::add_node() 构建菜单。
代码示例
function wp_admin_bar_my_account_menu( $wp_admin_bar ) {
$user_id = get_current_user_id();
$current_user = wp_get_current_user();
if ( ! $user_id ) {
return;
}
if ( current_user_can( 'read' ) ) {
$profile_url = get_edit_profile_url( $user_id );
} elseif ( is_multisite() ) {
$profile_url = get_dashboard_url( $user_id, 'profile.php' );
} else {
$profile_url = false;
}
$wp_admin_bar->add_group(
array(
'parent' => 'my-account',
'id' => 'user-actions',
)
);
$user_info = get_avatar( $user_id, 64 );
$user_info .= "{$current_user->display_name}";
if ( $current_user->display_name !== $current_user->user_login ) {
$user_info .= "{$current_user->user_login}";
}
if ( false !== $profile_url ) {
$user_info .= "" . __( 'Edit Profile' ) . "";
}
$wp_admin_bar->add_node(
array(
'parent' => 'user-actions',
'id' => 'user-info',
'title' => $user_info,
'href' => $profile_url,
)
);
$wp_admin_bar->add_node(
array(
'parent' => 'user-actions',
'id' => 'logout',
'title' => __( 'Log Out' ),
'href' => wp_logout_url(),
)
);
}注意事项
- 函数在 WordPress 3.1.0 版本中引入,是核心功能的一部分。
- 如果用户未登录($user_id 为 0),函数会直接返回,不添加任何菜单项。
- 在 Multisite 环境中,无 'read' 权限的用户会重定向到仪表板个人资料页面,确保兼容性。
- 菜单项使用 __() 函数进行国际化,支持多语言翻译。
原文内容
Adds the “My Account” submenu items.
Parameters
$wp_admin_barWP_Admin_Barrequired-
The WP_Admin_Bar instance.
Source
function wp_admin_bar_my_account_menu( $wp_admin_bar ) {
$user_id = get_current_user_id();
$current_user = wp_get_current_user();
if ( ! $user_id ) {
return;
}
if ( current_user_can( 'read' ) ) {
$profile_url = get_edit_profile_url( $user_id );
} elseif ( is_multisite() ) {
$profile_url = get_dashboard_url( $user_id, 'profile.php' );
} else {
$profile_url = false;
}
$wp_admin_bar->add_group(
array(
'parent' => 'my-account',
'id' => 'user-actions',
)
);
$user_info = get_avatar( $user_id, 64 );
$user_info .= "<span class='display-name'>{$current_user->display_name}</span>";
if ( $current_user->display_name !== $current_user->user_login ) {
$user_info .= "<span class='username'>{$current_user->user_login}</span>";
}
if ( false !== $profile_url ) {
$user_info .= "<span class='display-name edit-profile'>" . __( 'Edit Profile' ) . '</span>';
}
$wp_admin_bar->add_node(
array(
'parent' => 'user-actions',
'id' => 'user-info',
'title' => $user_info,
'href' => $profile_url,
)
);
$wp_admin_bar->add_node(
array(
'parent' => 'user-actions',
'id' => 'logout',
'title' => __( 'Log Out' ),
'href' => wp_logout_url(),
)
);
}
Changelog
| Version | Description |
|---|---|
| 3.1.0 | Introduced. |