WordPress Login/Logout Link for Navigation

Recently i wanted to add a login/logout link to one of the menus of my wordpress theme. I wasn’t able to find a nice solution for this. So i wrote down a thow to solve this for you guys. You just have to add the following link to you functions.pgp file and make the adjustments fitting to your needs. In the code you can also define the redirect link for the login and logout pages.

//add login and logout code to your menu
function add_loginout_link( $items, $args ){
 
//insert your css menu id at my-menu-id
if( is_admin() ||  $args->menu_id != 'my-menu-id' )
return $items;
//insert your redirect link to 'a href=""'
$redirect = ( is_home() ) ? false : get_permalink();
if( is_user_logged_in( ) )
$link = '<a href="' . wp_logout_url( $redirect ) . '" title="' .  __( 'login' ) .'">' . __( 'login' ) . '</a>';
else  $link = '<a href="' . get_site_url() . '" title="' .  __( 'logout' ) .'">' . __( 'logout' ) . '</a>';
 
return $items.= '<li id="loginout-link" class="menu-item menu-type-link">'. $link . '</li>';
}
add_filter( 'wp_nav_menu_items', 'add_loginout_link', 50, 2 );
About This Author

Grüezi! My name is Angela. I'm projectleader of the swissbased webagency Tree Stones, i love fantasybooks and you could say i'm in a unhealty realtionship with coffe from Starbucks.

1 Comment

You can post comments in this post.


  • Informative article, just what I wanted to find.

    Laura 9 years ago Reply


Post A Reply

*