0

Using this code:

add_filter( 'wp_nav_menu_items', 'my_custom_menu_item');
function my_custom_menu_item($items)
{
    if(is_user_logged_in())
    {
        $user=wp_get_current_user();
        $name=$user->display_name; // or user_login , user_firstname, user_lastname
        $items .= '<li><a href="">Welcome '.$name.'</a></li>';
    }
    return $items;
}

I am able to display the current logged in user's display Name into the navigation, but it replaces the current menu items.

What would I need to make the Display name the parent item which I can then use the wordpress menu system for the sub items ? I assume id need some kind of placeholder in the menu like #place_holder# but am unsure how to get to that stage.

1

Works fine for me, nothing is replaced, it's just appended to the navigation. Are you sure that it's not just a styling-issue? Have you looked at the document source to check if it really is replacing the other menu items?

What exactly do you mean by making it the parent item? Do you want to turn

<li><a href="">Item</a></li>

into

<li><a href="">Welcome $user</a>
    <ul>
        <li><a href="">Item</a></li>
    </ul>
</li>

in the end?

To change your menu and make it easy & maintainable, maybe you could use something like this:

add_filter( 'wp_nav_menu_objects', 'my_custom_menu_item');
function my_custom_menu_item($items) {
    $remove_childs_of = array();
    foreach($items as $index => $item) {
        if($item->title == "##currentuser##") {
            if(is_user_logged_in()) {
                $user=wp_get_current_user();
                $name=$user->display_name; // or user_login , user_firstname, user_lastname
                $items[$index]->title = $name;
            }
            else {
                array_push($remove_childs_of, $item->ID);
                unset($items[$index]);
            }
        }
        if(!empty($remove_childs_of) && in_array($item->menu_item_parent, $remove_childs_of)) {
            array_push($remove_childs_of, $item->ID);
            unset($items[$index]);
        }
    }
    return $items;
}

The wp_nav_menu_objects filter allows us to work on the items before they get transformed into HTML. You'll have to add an Item to your menu that shows "##currentuser##". If a user is logged in, this menu item will show his name afterwards. If it is an anonymous user, the menu item and its submenues will be removed from the navigation.

  • Initially it was a cache problem, so my menu is still working but the above code just adds the username currently. Ideally id like the "Welcome User" as a parent of a dropdown menu. Then I can add my other pages such as Dashboard, Edit profile etc. So the menu acts like a user menu. – Randomer11 Oct 9 '17 at 20:55
  • see my updated answer, I believe that is what you're after. – janh Oct 9 '17 at 21:14
  • Absolutely perfect, thank you very much janh2 . Much appreciated – Randomer11 Oct 9 '17 at 21:18
  • Sorry to bring this up again, But is there anyway of adding a logout item at the bottom of the dropdown menu ? I currently insert the logout on the main menu but it looks weird with the username dropdown, so adding it to the username dropdown as the last item would be ideal. Sorry to bother you Janh2 – Randomer11 Oct 19 '17 at 19:43
  • Sure. In the end, it's just an array of Items. Add one for LogOut and replace its URL with WP's wp_logout_url(). I think you can recycle a lot of the logic, you just need to look for another item. – janh Oct 19 '17 at 20:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.