So one of the things I was looking into was how to add a login/logout link to th This e Navigation. However, when I first did it, I did it to all of them; however, I finally figured out how to create this link, as seen in the footer, in the menu based on the menu. This tutorial targets Genesis users but non-Genesis users can use the same code with the deletion of the last line.
All Genesis themes have Primary and Secondary menus registered in the Genesis engine.
{DO NOT ADD THIS CODE ANYWHERE: DEMO ONLY}
[php]<?php
register_nav_menus( array(
'primary' => __('Primary Navigation Menu', 'genesis'),
'secondary' => __('Secondary Navigation Menu', 'genesis')
) );
?>[/php]
As you can see the two names are primary and secondary in lower case. One other Genesis child theme, News, registers a menu called topnav.
You will need to add the following to your functions.php file based on where you want the link to occur.
[php highlight="4,5,6,7,8,9,10,11"]
<?php
function wps_add_loginlogout_link($items, $args)
{
//defaults; change this as you see fit
$defaults = array(
'loginlout_position' => 999, //enter 0 for front, 999 for end
'menu_id' => 'menu-item', //custom CSS
'menu_class' => 'menu-item menu-item-type-custom menu-item-object-custom', //custom CSS
'menu_location' =>'primary', //enter primary, secondary, or topnav for News
'loginredirect' => false //enter true for redirect to wp-admin dashboard
);
//do nothing if not proper menu location
if( $args->theme_location != $defaults['menu_location'] )
return $items;
//set redirect URL
if( $defaults['loginredirect'] )
$wpurl = 'wp-admin/index.php';
else
$wpurl = 'index.php';
// split the menu items into an array using the ending <li> tag
if ( $defaults['loginlout_position'] != 0 && $defaults['loginlout_position'] != 1 && $defaults['loginlout_position'] != 999 ) {
$items = explode('</li>',$items);
}
if(is_user_logged_in())
{
$newitem = '<li id="'.$defaults['menu_id'].'" class="'.$defaults['menu_class'].'"><a title="Logout" href="'. wp_logout_url($wpurl) .'">Logout</a></li>';
if ( $defaults['loginlout_position'] == 0 || $defaults['loginlout_position'] == 1 )
$newitems = $newitem.$items;
elseif ( $defaults['loginlout_position'] == 999 )
$newitems = $items.$newitem;
else
$newitem = '<li id="'.$defaults['menu_id'].'" class="'.$defaults['menu_class'].'">' . $args->before . '<a title="Logout" href="'. wp_logout_url('index.php') .'">' . $args->link_before . 'Logout' . $args->link_after . '</a>' . $args->after; // no </li> needed this is added later
}
else
{
$newitem = '<li id="'.$defaults['menu_id'].'" class="'.$defaults['menu_class'].'"><a title="Login" href="'. wp_login_url($wpurl) .'">Login</a></li>';
if ( $defaults['loginlout_position'] == 0 || $defaults['loginlout_position'] == 1 )
$newitems = $newitem.$items;
elseif ( $defaults['loginlout_position'] == 999 )
$newitems = $items.$newitem;
else
$newitem = '<li id="'.$defaults['menu_id'].'" class="'.$defaults['menu_class'].'">' . $args->before . '<a title="Login" href="'. wp_login_url('index.php') .'">' . $args->link_before . 'Login' . $args->link_after . '</a>' . $args->after; // no </li> needed this is added later
}
if ( $defaults['loginlout_position'] != 0 && $defaults['loginlout_position'] != 1 && $defaults['loginlout_position'] != 999 ) {
$newitems = array();
// loop through the menu items, and add the new link at the right position
foreach($items as $index => $item)
{
// array indexes are always one less than the position (1st item is index 0)
if($index == $defaults['loginlout_position']-1)
{
$newitems[] = $newitem;
}
$newitems[] = $item;
}
// finally put all the menu items back together into a string using the ending <li> tag and return
$newitems = implode('</li>',$newitems);
}
return $newitems;
}
add_filter('wp_nav_menu_items', 'wps_add_loginlogout_link', 10, 2);
add_filter('genesis_nav_items', 'wps_add_loginlogout_link', 10, 2); //non-Genesis users delete this line.
?>
[/php]
Bruce says
Very nice!
I’ve been looking for a way to do this. The feature to add it in the desired position is great. This must have taken some time to come up with. Nice job!
Mark Sullivan says
Thanks for this.
Spent at least 2 hours searching for some proper help on this. You nailed it.
Might be worth pointing out to other noobs, that the default menu name must be changed as per the registered menu names. Caught me out for 10mins or so. Seems so obvious now.
Thanks
Ali says
this sounds promising but i couldn’t do it i don’t know why!
i added this piece of code in the functions.php file but nothing happened… i don’t know what exactly should be edited here too. can you please explain more?
i’ll be very thankful
wpsmith says
Hello Ali,
Simply set the defaults (lines 4-11) to whatever you’d like. Please let me know if you need anything else.
Thanks,
Travis
Efrem says
Ditto what Mark said above on all counts, but mostly, thank you for this great code.
I too spent the last two hours searching the Studio Press forums and Google trying to find something that worked this well on a single nav menu instead of having the link duplicated on the primary and secondary nav.
-Efrem
hamza khan says
thanks you so much !!
Fouad says
hey mate great code! many thanks!
but i have a question..
im using profile builder script which give me shortcodes to add login/register/edit profile forms in a page i make.. so my question is can i make the login button goes to the page i created with the login form ? instead of going to wp-login.php the wordpress login page?
Thanks Again!
Fouad says
i changed in line 40
$newitem = ‘Login‘;
and it worked perfect on wamp when i tried it
but when i added it to my website function.php i get this error while cliclink on Login
Warning: Cannot modify header information – headers already sent by (output started at /home/fudcom/public_html/wp-content/themes/duotive-fortune/functions.php:25) in /home/fudcom/public_html/wp-login.php on line 349
Warning: Cannot modify header information – headers already sent by (output started at /home/fudcom/public_html/wp-content/themes/duotive-fortune/functions.php:25) in /home/fudcom/public_html/wp-login.php on line 361
Pffffff thats so weird ! it works on my wamp but not on the website and i have the same plugins installed
do u have any idea ?:(
Fouad says
sorry the code i posted didnt appear… i changed on the line 40 this thing inside the href ‘. wp_login_url($wpurl) .’ and replaced it with my page link and worked perfect on wamp! but not on website :/
Luis says
I got the same error. Could you solve it? How?
Travis Smith says
Yes you can, and this code is quite beyond what you need.
Dallas Peters says
With this code is it possible to insert an array for the ‘menu_location’ =>’utility_navigation’ to have it spit out the same code for inserting in two locations?
I’m serving up two different menus for logged in and logged out users but would love for this link to show up in both!
Travis Smith says
Yes, change line 9 to:
[php]
‘menu_location’ => array( ‘primary’, ‘secondary’ ), //change to whatever your menu ids are
[/php]
And then line 14 to:
[php]
if( in_array( $args->theme_location, $defaults[‘menu_location’] ) )
[/php]
David A. Pimentel says
Actually, line 14 should be
if( ! in_array( $args->theme_location, $defaults[‘menu_location’] ) )
Otherwise, the feature will be missing from the desired menus.
Thanks for code. It works very well.
Jonathan says
Thanks Travis! Really helpful.
Andy Freist says
Can this be used to work for a menu that is in the Custom Menu Widget? Trying to get this to work for the header nav (which uses the widget) in the Minimum Pro genesis theme..
Any insight would be appreciated!
Travis Smith says
Yes but the Custom Menu Widget sadly does not have a location, which it should in my opinion. Nonetheless, it can but may require some fancy checking. Please let me know if you need more help on this.
Martin Haranta says
Hi, great job with the code, i have just one question how can i remove this login/logout from several particular pages? e.g. i have it on my secondary navigation but i want to be show only on six pages from this secondary navigation. How can i do that?
Thanks
Martin Haranta says
Hi, great job with the code, i have just one question how can i remove this login/logout from several particular pages? e.g. i have it on my secondary navigation but i want to be show only on six pages from this secondary navigation. How can i do that? –
Thanks
Laza says
Great tuts however, I think that you should start using pasbin/git or some way to better show off your code.
mani says
hi i cant find good solution to add login logout menu for membership in my website…could you help me