Last active
February 16, 2017 08:54
-
-
Save dkjensen/40720f200dfc60136569c1d38fb4bb51 to your computer and use it in GitHub Desktop.
WordPress - Display logged in users name in menu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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