wordpress snippets last updated   : September 29, 2014
we now have 639 snippets

Add a login link to the wp_nav_menu

wordpress snippet

Adding this snippet to the functions.php of your wordpress theme will add a login link to your wp_nav_menu or if the user is already logged in a logout link.

snippet :  PHPcopy
add_filter('wp_nav_menu_items', 'add_login_logout_link', 10, 2);
function add_login_logout_link($items, $args) { 
        $loginoutlink = wp_loginout('index.php', false); 
        $items .= '<li>'. $loginoutlink .'</li>'; 
    return $items; 
}

Did you like this post? If so please share it with your friends. Thanks!

  • prince shah

    hey kevin thank you very much for this code but i also want to add register link, plz suggest me how to do it.
    Actually i modified the code to :-
    add_filter(‘wp_nav_menu_items’, ‘add_login_logout_link’, 10, 2);
    function add_login_logout_link($items, $args) {

    $registerlink = wp_register(”, ”);
    $loginoutlink = wp_loginout(‘index.php’, false);
    $items .= ”. $loginoutlink .”.”. $registerlink .”;
    return $items;
    }

    but its not working.

    • http://wpsnipp.com Kevin Chard

      change
      $items .= ”. $loginoutlink .”.”. $registerlink .”;

      to
      $items .= ”. $loginoutlink .”. $registerlink .”;

      • prince shah

        thanks fo ua help buddy, but actually i sorted it on own.

        • http://wpsnipp.com Kevin Chard

          cool glad to hear it!