I got a requirement to update the WordPress's admin panel, where I need to display the current user's avatar on the top of admin's main menu(see the screenshot).
-
1Welcome to WPSE. What have you tried? Questions in the form of "do this for me" are not typically answered.– jdm2112Jul 14, 2021 at 5:58
-
I found this set of code to display the icon on the menu, I want to display a user's avatar but I couldn't find the way. I need to know if there is possibility to do without touching the WP core..– princeexpeditionJul 14, 2021 at 6:03
-
1100% you should not edit WP core files. The first update that comes along and your changes are gone. Luckily with hooks and filters you can extend WP millions of ways. What is the code you posted NOT matching your requirements? Simplify your task to a specific question that can be answered.– jdm2112Jul 14, 2021 at 6:09
-
Yes, You are right. Anyways, I removed my code. Indeed it is very strange that I have been trying to find the solution all over the WordPress blog but I couldn't any relevant details :(– princeexpeditionJul 14, 2021 at 6:17
1 Answer
I'm not sure if core offers any hooks well suited to this purpose. I certainly don't see any purpose-fit hooks in the source which renders the admin menu.
But on a more hacky whim, this could be relatively easily accomplished using a add_menu_item()
with $position
0
, then modifying the new menu item registration in the $menu
global in order to adjust any parameters as necessary.
function wpse391894_admin_menu_avatar() {
global $menu;
$user = wp_get_current_user();
$title = sprintf(
__( 'Howdy, %s', 'wpse391894' ),
esc_html( $user->display_name )
);
$img = get_avatar( $user );
add_menu_page(
$title,
sprintf(
"%s\n%s",
$img,
$title
),
'read',
'wpse391894-dummy-slug',
'',
'none',
0
);
// Look up a reference to the new menu item registration array.
$item = &$menu[ array_key_last( $menu ) ];
// Modify the link?
$item[2] = "user-edit.php?user_id={$user->ID}";
}
add_action( 'admin_menu', 'wpse391894_admin_menu_avatar' );