Site icon WP Smith

How to Add a Login/Logout Link to Your WordPress Genesis Custom Menu Based on Menu Location

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]