Display_name in menu

I’m trying to display the “Display_name” of an user in the menu. So I created a menu item called “#profile_name#”.

It’s working when i’m logged, but it displays “Untitled” when i’m not logged. Do you know why ?

function give_profile_name(){
$user=wp_get_current_user();
$name=$user->display_name;
return $name;
}

add_shortcode(‘profile_name’, ‘give_profile_name’);

add_filter( ‘wp_nav_menu_objects’, ‘my_dynamic_menu_items’ );
function my_dynamic_menu_items( $menu_items ) {
foreach ( $menu_items as $menu_item ) {
if ( ‘#profile_name#’ == $menu_item->title ) {
global $shortcode_tags;
if ( isset( $shortcode_tags[‘profile_name’] ) ) {
// Or do_shortcode(), if you must.
$menu_item->title = call_user_func( $shortcode_tags[‘profile_name’] );
}
}
}

return $menu_items;
}

Thank you

Read more here:: Display_name in menu

Leave a Reply

Your email address will not be published. Required fields are marked *