Site icon WP Smith

How to Add a Log In/Log Out Link to a Specific Custom Menu

If you are like me, you may use a framework or theme that supports multiple menu locations. In a previous post, I discussed how to add a login or logout menu item based on menu location. However, if you use the custom menu widget, this tutorial won't help you.

Also, if you are like me, you may want to be able to customize the Log In or Log Out to Sign In or Sign Out. So to add a customized log in or log out menu item at the end of the menu, add this to your functions.php:

[php]
add_filter('wp_nav_menu_items', 'wps_add_login_logout_link', 10, 2);
function wps_add_login_logout_link($items, $args) {
$login = __('Sign in');
$logout = __('Sign out');

//use one of the following methods of identification
$menu_id = '34';
$menu_name = ''; //name you gave to the menu
$menu_slug = ''; //slug of the menu, generally menu_name reduced to lowercase

if ( ! is_user_logged_in() )
$link = '<a href="' . esc_url( wp_login_url($redirect) ) . '">' . $login . '</a>';
else
$link = '<a href="' . esc_url( wp_logout_url($redirect) ) . '">' . $logout . '</a>';

if ( ($menu_id) && ($args->menu->term_id == $menu_id) )
$items .= '<li>'. $link .'</li>';
elseif ( ($menu_name) && ($args->menu->name == $menu_name) )
$items .= '<li>'. $link .'</li>';
elseif ( ($menu_slug) && ($args->menu->slug == $menu_slug) )
$items .= '<li>'. $link .'</li>';

return $items;
}
[/php]