0

I need to add "Login" to main nav-menu. with sub menu "register" (link http://www.vcanade.ca/register/)

After user logged in, "Login" has to change to his username with sub menus "profile" (link http://www.vcanade.ca/your-profile/) and "Logout"

Is there way to do this?

So far I did only to display logged in user name by adding to fuction.php

add_filter( 'wp_nav_menu_items', 'your_custom_menu_item', 10, 2 );
function your_custom_menu_item ( $items, $args ) {  
    $current_user = wp_get_current_user();
    if(!empty($current_user->user_login))
        $items .= '<li><a href="javascript:void(0)">'.$current_user->user_login.'</a></li>';        
    return $items;
}
0

Here is a barebones example (which I just tested on my site):

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

function asv_add_usermenu( $items, $args ) {
  if (is_user_logged_in() && $args->theme_location == 'primary-menu') {
    $items .= "<li class='sub-menu'><a href='" . wp_logout_url()  . "'>Log Out</a></li>";
  } elseif (!is_user_logged_in() && $args->theme_location == 'primary-menu') {
    $items .= "<li class='sub-menu'> <a href='" . wp_login_url() . "'>Log In</a></li>";
  }
  return $items;
}

The 'primary-menu' text is from this admin page (whatever name the theme registered that menu as). yoursite.com/wp-admin/nav-menus.php It is one of the menu names in grey italics in the top left box.

I gleaned the above from http://codex.wordpress.org/Function_Reference/wp_get_nav_menu_items and codex.wordpress.org/Function_Reference/wp_nav_menu and I think the 'primary-menu' is the only thing environment specific.

  • Thanks a lot. i did everything according you instruction, but new item menu does not appear in my menu at all. 'primary-menu' i changed to 'menu1' Any idea? – Vladimir Kondrachov Aug 31 '15 at 20:58

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.