Add Hi, Username! to menu, I have the username working, but not the Hi part

The following code will allow me to display the username in the menu as an item in the menu, which means I can move it around in the menu, but I am unable to add a greeting.. ie., Hi, username! This would be ideal if I could just add text in front of the username..

To use this, you have to place #profile_name# as the navigation label in the custom link in the menu to display the username, just cant figure out how to get Hi, username!

function give_profile_name($atts){
$user=wp_get_current_user();
$name=$user->user_login;
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;
}

any help on this would help my wall out.. and my head.

  • Bojan Radonic
    • Head of Support

    Hey there ME Admin,

    How are you doing today?

    Can you please try the following instead:

    function give_profile_name($atts){
    $user=wp_get_current_user();
    $name=$user->user_login;
    return sprintf(__("Hi, %s","domain"),$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;
    }

    Result should be "Hi, username".

    Hope this helps :slight_smile:

    Cheers,
    Bojan

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.