Add a login/logout link to a WordPress navigation menu

Screen Shot 2013-09-12 at 6.03.09 PM

I was looking for a way to add a login/logout link to a WordPress menu a few months back and I was not able to find it a good solution anywhere, so here it is. I also summited an idea to WordPress to see if they can add the option on the admin menu area where you can select it just like you select the homepage link.

Now, there are a few posts that show you how to do it, but they over complicate things, they also ask you to use “ob_start()” function that you don’t need at all.

No plugin needed, add it to the theme’s functions.php file

Add the following code to your theme’s functions.php file and change the menu area name if you are not using “primary“. For WordPress 3.0 and up.

//Add login/logout link to naviagation menu
function add_login_out_item_to_menu( $items, $args ){

	//change theme location with your them location name
	if( is_admin() ||  $args->theme_location != 'primary' )
		return $items; 

	$redirect = ( is_home() ) ? false : get_permalink();
	if( is_user_logged_in( ) )
		$link = '<a href="' . wp_logout_url( $redirect ) . '" title="' .  __( 'Logout' ) .'">' . __( 'Logout' ) . '</a>';
	else  $link = '<a href="' . wp_login_url( $redirect  ) . '" title="' .  __( 'Login' ) .'">' . __( 'Login' ) . '</a>';

	return $items.= '<li id="log-in-out-link" class="menu-item menu-type-link">'. $link . '</li>';
}add_filter( 'wp_nav_menu_items', 'add_login_out_item_to_menu', 50, 2 );

29 Comments on “Add a login/logout link to a WordPress navigation menu

  1. Haxsays:

    @ ribeiro,

    instead of using “wp_logout_url” function you can hard-code the urls in the anchor (a) tags.

  2. Haxsays:

    @vrsotto,

    you will have to change this line base on the user role.
    $redirect = ( is_home() ) ? false : get_permalink();

  3. georgesays:

    I cannot get it to work. I thought maybe my issue is the name of the menu, so, I changed it, to my menu name. “main menu” then “MAIN_MENU” then “main_menu”. and so on.. nothing..

    I even changed the name of my menu to primary, just in case I was going crazy.. that didn’t work

    I did end up getting the OB method to work, but, I don’t think that is as elegeant.

    any idea why i’m not getting this to work? could it be some other issue in my wordpress? how to debug my problem?

  4. Xpark Mediasays:

    @george,

    It could be an issue with your theme. Try:

    – Change 50 to 100 in the “wp_nav_menu_items” filter.
    – Remembre to change theme_location, this is the location of the menu not the menu name.

  5. Bobysays:

    Hi, sorry to bother ..
    I have a custom menu called “my menu” and it’s located in the “Main Menu”
    I inserted the code in my functions file and changed ‘primary’ to ‘Main Menu’.
    But i can’t see any change in my menu.. Do i have to add it to my meny afterwards?
    I also tried changing 50 to 100 but no luck..
    Thanks

  6. Bobysays:

    Got it working. Had to look for the proper name in the functions file where the menu was registered with a different caption name (Main Menu) but main_nav instead and it works now. Awesome.
    Thanks for the code 🙂 should make it a plugin :))

  7. Xpark Mediasays:

    @KarmaTiger,

    you may have to add additional classes to support your theme mobile menu.

  8. Jasonsays:

    Changing the redirect from

    $redirect = ( is_home() ) ? false : get_permalink();

    to

    $redirect = “http://xparkmedia.com/”

    Causes a parse error. Can you tell me what to fix? Without the change, the login link URL is my website’s home page. Shouldn’t it be something like ‘http://www.mydomain.com/wp-login.php?

  9. Jasonsays:

    Fixed the hard coded url, it was my typo problem. However the login simply goes to my home page and not the expected ‘wp-login.php’ page? Am I missing something? Many thanks for help.

  10. Xpark Mediasays:

    @jason,

    $redirect is just to redirect the user after they have logged in. If you don’t want to redirect them just use:

    $redirect = false; the login url is created by wp_logout_url()


Leave a Reply

Your email address will not be published. Required fields are marked *