WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am new to Wordpress I am setting up wordpress Site which has 3 Menus

  • Top menu
  • Header Menu
  • Footer Menu

My issue is I need to add the User name to My Top menu Only when User is Logged in

But using below Code snippet it adds the Custom menu Item in all the Menus the Below Code i found while googleing the Solution for the above Query

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

This is my Top menu Structure

if User is not logged in

  • My Account
    • Login
    • Signup
    • Lost Password

if User is Logged In

  • Welcome Nikhil
    • Profile
    • Change Password
    • Logout
    • blogs
share|improve this question
up vote 1 down vote accepted

wp_nav_menu_items filters passes menu arguments as second parameter.

So if you modify code to this:

add_filter( 'wp_nav_menu_items', 'my_custom_menu_item', 10, 2);

function my_custom_menu_item($items, $args) ...

You will be able to check data $args inside function to match specific menu. However the specifics of what to check for depend on how precisely your menus are set up. Might be $args['menu'] holding name, might be $args['theme_location'], etc.

Another option is to simply add your callback right before the needed menu is called and remove right after in theme template:

add_filter( 'wp_nav_menu_items', 'my_custom_menu_item');

// target wp_nav_menu() call in between

remove_filter( 'wp_nav_menu_items', 'my_custom_menu_item');
share|improve this answer
Hey Thanks Kudos to you can you guide me more on this Creating the Structure as Above – Nikhil Joshi Sep 19 '13 at 13:01
Can you give the Example for the "Another Option" – Nikhil Joshi Sep 19 '13 at 13:16

@Rarst is right, you need the second argument to do this only for an specific menu, in your example will be something like:

function my_custom_menu_item($items, $args)
    if(is_user_logged_in() && $args->theme_location == 'primary')
        $name=$user->display_name; // or user_login , user_firstname, user_lastname
        $items .= '<li><a href="">Welcome '.$name.'</a></li>';
        $items.= //Change Password, Logout, etc
    elseif (!is_user_logged_in() && $args->theme_location == 'primary') {
        $items .= '<li><a href="'. site_url('wp-login.php') .'">Log In</a></li>';
        $items.= //Sign up, Lost Password
    return $items;
add_filter( 'wp_nav_menu_items', 'my_custom_menu_item');
share|improve this answer
yeah i know tom but i wanted the Welcome or My account Text be on the parent Li and then the Children (Sign up , Change Password) for that i need to write as $items .= '<li><a href="">Welcome '.$name.'</a>'.$items.'</li>'; – Nikhil Joshi Oct 4 '13 at 11:23
In that case you should use something like: $items .= '<li><a href=""> Welcome' .$name. '</a> <ul>'.$items.' </ul></li>'; – Tomás Ramírez Oct 4 '13 at 18:33
Hmm got it thanks :) – Nikhil Joshi Oct 5 '13 at 5:28

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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