Add a Login Logout Link to Only One Specific Menu

A while back we had a nice little post one here about how to add a login/logout link to your nav menu. I recommend reading that through first if you haven’t before. It’ll get you up to speed and ready for this next tidbit of information. Today we will expand on that idea, with a login/logout link added to only one menu, with a referrer redirect! I’ll explain it all next.

That previous tutorial simply added in a simple login/logout link. The problem is, if you have a theme with multiple menus, the link will be on all of them. We can conditionalize for that, and only add it to one menu.

//ADD LOGIN LINK TO MENU
add_filter('wp_nav_menu_items', 'voodoo_loginout_link', 10, 2);

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

So let’s take a look at what’s going on now. Up top in line 2 there, we simply hook onto wp_nav_menu_items with our voodoo_loginout_link custom function, which we start to define on line 3.

Line 4 is how we tell this to only happen on a certain menu. You will need to take a look at what menu you want to add to. Usually in functions.php all of your menus are registered using register_nav_menus. And in your templates you will find wp_nav_menu calling to one of the menus by theme-location. It’s that bit of info you need to input on line 4 here, in my example, it’s main-menu.

Line 5 here we set up a variable called $voodooredir, and it holds the referring page. Whatever page someone was on when the click the login link is the page they will go back to. You can leave this out and modify line 6 if you like. If you look back at that original tutorial, you’ll see we redirected to index.php.

In this example, line 6 sets up another variable, this one called $loginoutlink. It simply holds wp_loginout, and our redirection variable. The false here just tells wp to return the link as a string rather than to echo out the link. You could set it to true if you were using wp_loginout to create a button or something and needed the actual URL output.

On line 7 we do our first return, which is inside the conditional statement. So we return $items (our original menu items) with $loginoutlink appended to it if the conditional is true. Which would mean if we are on our desired menu, main-menu in my case.

Line 9 is outside the conditional, so this is what returns if the statement is false. In our case here, we return $link (our original menu items) unmodified on any menu not main-menu.

7 Replies to “Add a Login Logout Link to Only One Specific Menu”

  1. It worked for me! Only that I had to replace the menu location from ‘main-menu’ to ‘primary’. Thank you so much! I appreciate the help!

    My second request, I think it also has to do with this topic.
    Once I login, I want an additional external link to be displayed. (Ex. After login, show Mail link) right next to the Logout link (again, in the same specific menu).
    How can it be done.

    1. OK, so you want a link, only when someone is logged in, correct? Is it a link to one of your WP pages, or just a link in general?

      //ADD LOGIN LINK TO MENU
      add_filter('wp_nav_menu_items', 'voodoo_loginout_link', 10, 2);
      
      function voodoo_loginout_link($items, $args) {
      		$voodooredir = htmlspecialchars($_SERVER['REQUEST_URI']);
      		$loginoutlink = wp_loginout($voodooredir, false);
      	if(is_user_logged_in() && $args->theme_location == 'primary' ) {
      		return $items . "<li> $loginoutlink </li><li><a href="http://www.rvoodoo.com">Testing</a></li>";
      	}
      	elseif (!is_user_logged_in() && $args->theme_location == 'primary') {
      		return $items . "<li> $loginoutlink </li>";
      	}
      	return $items;
      }
      

      Should be what you need. You’ll just need to change out where I have my blog URL in there for your own URL. Be careful to keep those backslashes in there, to keep your site from crashing!

  2. I’m having a tough time figuring out a problem I’m having. I need a link on a specific menu, but it’s being called via the custom menu widget. The theme-location won’t work and I’m trying various forms of if ( $args->menu = 3 ), but it either adds the link to every menu or none.

    Any suggestions, pointers, etc?

      1. Thanks very much for stopping back by with your solution! Much appreciated! I’ll leave your comments, as they could be quite valuable to a user down the line with a similar situation.

Leave a Reply