10 Replies to “Add a Simple Login Link To Your Nav Menu With Homepage Redirect”

  1. My problem with this is that it STILL shows up in both my navigation menus. I have a top menu, the primary, and a regular horizontal menu right below it , which i take to be the secondary menu. I want the loginlout links to appear ONLY in the primary nav menu. But it’s not doing it.

    1. Oh, you have to conditionalize for that in the code!

      //ADD LOGIN LINK TO MENU
      add_filter('wp_nav_menu_items', 'add_login_logout_link', 10, 2);
      
      function add_login_logout_link($items, $args) {
      	if( $args->theme_location == 'main-menu' ) {
      	$voodooredir = htmlspecialchars($_SERVER['REQUEST_URI']);
      	$loginoutlink = wp_loginout($voodooredir, false);
      	return $items . "<li> $loginoutlink </li>";
      	}
      	return $items;
      }
      

      There’s an example where I return the user to whatever page they came from, only added to my menu location main-menu

      1. Wasn’t working for me, but after a few changes it worked:

        add_filter(‘wp_nav_menu_items’, ‘add_login_logout_link’, 10, 2);
        function add_login_logout_link($items, $args) {
        if( $args->menu == ‘Menu_Footer’ ) {
        ob_start();
        $loginoutlink = wp_loginout(‘index.php’, false);
        $items .= ”. $loginoutlink .”;
        }
        return $items;;
        }

  2. Omg omg! It worked! It worked! What happened?!! Tell me!

    Also, if you dont mind, how do I make a conditional, where when one’s logged in and an extra link is displayed right next to the logout link? (i would TRULY appreciate the help)

  3. I am now part of the Voodoo Empire! Thanks so much for posting this, you have no idea how much you cleared up about wp_nav_menu() and filter hooks. You get a gold star for today and how long ago did you write this post? Very nice work, Reverend.

    1. I’m glad you enjoyed the tutorial! I wrote this in February of 2011. You can see a few comments above another example – if you ever use a theme which has multiple menus, you would want to specify a certain menu – unless you want the login/out link to appear in all menus! You can use this same technique to add various things to your menu… I’ve done a search form before, and various other things. Once you understand the concepts it isn’t so hard!

Leave a Reply