Blank placeholder on Navigation Menu

Basically, I am trying to place a blank placeholder on Navigation Menu. So that when the user logs in it will display the user name otherwise it will stay blank. I added this function to do so -

function give_profile_name(){
    $user=wp_get_current_user();
	if(!is_user_logged_in()){
        $name = " ";
	} else {
         $name=$user->user_firstname; 
	}
    return $name;
}

Basically, I first named the menu item as #user_name# and used the filter -

add_shortcode('user_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 ( '#user_name#' == $menu_item->title ) {
            global $shortcode_tags;
            if ( isset( $shortcode_tags['user_name'] ) ) {
                // Or do_shortcode(), if you must.
                $menu_item->title = call_user_func( $shortcode_tags['user_name'] );
            }    
        }
		
    }

    return $menu_items;
} 

I tested this with twenty14 theme and it is working as expected. But on this theme I am seeing UNTITLED on the menu bar instead of blank when not logged in. However, on login it is displaying the correct username. Please advice.

Hi @dgarcia210,

I hope you are well today and thank you for your question.

You can try achieving this by using the attached custom child theme of Shapely theme that contains some custom code.

Best Regards,
Movin

Thanks! a lot @Movin… you are awesome. It works perfectly. :slight_smile:

You are most welcome here :slight_smile: