Support » Developing with WordPress » Add login/logout link to existing menu

  • Resolved ctuxboy

    (@ctuxboy)



    Hello,

    Try adding Login (when user not logged in) or logout (when user logged in) menu item.
    This works, but the login/logout menu items adding to all the menu’s.
    How can adding it for only the ‘primary‘ menu?

    
    function add_login_logout_link( $items, $args  ) {
        if( has_nav_menu( 'primary' ) ) {
    	    if ( is_user_logged_in() ) {
    	        $items .= '<li><a href="'.wp_logout_url( home_url() ).'" title="Logout">Logout</a></li>';
    	    } else {
    		    $items .= '<li><a href="https://website.com/my-account/" title="Login">Login or register</a></li>';
            }
    	return $items;
    	}
    }
    add_filter( 'wp_nav_menu_items', 'add_login_logout_link', 10, 2 );
    • This topic was modified 9 months ago by  ctuxboy.
Viewing 1 replies (of 1 total)
  • ctuxboy

    (@ctuxboy)

    I found it:

    function add_login_logout_link( $items, $args  ) {
        if( is_user_logged_in() && $args->theme_location == 'primary' ) {
    	    $items .= '<li><a href="'.wp_logout_url( home_url() ).'" title="Logout">Logout</a></li>';
        }
    	elseif (!is_user_logged_in() && $args->theme_location == 'primary') {
    		$items .= '<li><a href="https://website.com/my-account/" title="Login">Login or register</a></li>';
            }
    	return $items;
    	}
    add_filter( 'wp_nav_menu_items', 'add_login_logout_link', 10, 2 );
    • This reply was modified 9 months ago by  ctuxboy.
Viewing 1 replies (of 1 total)
  • The topic ‘Add login/logout link to existing menu’ is closed to new replies.