Skip to content

Instantly share code, notes, and snippets.

@dkjensen
Last active February 16, 2017 08:54
Show Gist options
  • Save dkjensen/40720f200dfc60136569c1d38fb4bb51 to your computer and use it in GitHub Desktop.
Save dkjensen/40720f200dfc60136569c1d38fb4bb51 to your computer and use it in GitHub Desktop.
WordPress - Display logged in users name in menu
/**
* Change the first item text in a WP menu to the users display name
*
* @param string $title The menu item's title.
* @param WP_Post $item The current menu item.
* @param stdClass $args An object of wp_nav_menu() arguments.
* @param int $depth Depth of menu item. Used for padding.
*/
function wp_nav_menu_display_name( $title, $item, $args, $depth ) {
if( is_user_logged_in() ) {
if( $args->theme_location == 'header_navigation' ) {
if( $item->menu_order == 1 ) {
global $current_user;
$title = sprintf( __( 'Welcome, %s!', 'text-domain' ), $current_user->display_name );
}
}
}
return $title;
}
add_filter( 'nav_menu_item_title', 'wp_nav_menu_display_name', 10, 4 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment