Home / functions.php / Add a login link to the wp_nav_menu
  • Add a login link to the wp_nav_menu

    add-a-login-link-to-the-wp_nav_menu

    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.

    wordpress snippet : PHP

    <>
    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;
    }
    

    ( WordPress codex functions, hooks, in this snippet. )

    loginout, wp_loginout, add_filter, wp_nav_menu, _n, wp,
  • 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!

Build a better with wpsnipp.com

Weekly snippets, themes, deals coupons roundup.

x No Thanks