Skip to content

Instantly share code, notes, and snippets.

@max-kk
Last active October 9, 2020 21:53
Show Gist options
  • Save max-kk/07efd029cb1a78c9dc0dbe3778e57f6f to your computer and use it in GitHub Desktop.
Save max-kk/07efd029cb1a78c9dc0dbe3778e57f6f to your computer and use it in GitHub Desktop.
LRM :: add useravatar and username to the menu
<?php
// COPY AFTER THIS LINE !!!
add_filter( 'wp_nav_menu_items', 'lrm_your_custom_menu_item', 10, 2 );
function lrm_your_custom_menu_item ( $items, $args ) {
// Please, don’t forget to replace the theme_location !
// if the id of my menu was main-menu, I would write if( $args->theme_location == 'main-menu' ) {
if( $args->theme_location == 'main-menu' ){
$current_user = wp_get_current_user();
if($current_user && !empty($current_user->user_login)) {
$items .= '<li><a href="javascript:void(0)">'
. get_avatar( $current_user->ID, 32 )
. ' '
. $current_user->user_login
. '</a></li>';
}
}
return $items;
}
/*
Optionally you could use:
$current_user->display_name
$current_user->first_name
$current_user->last_name
*/
@DanielGe
Copy link

DanielGe commented Oct 9, 2020

Hello,

I was not able to find place where to paste this? in which file?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment