Add WooCommerce MyAccount Page Login/Logout to Menu

This code adds a login/logout link in your WordPress primary menu to log the user in to the WooCommerce My Account page and then also logout to the same or different page.

<?php //<~ don't add me in
add_filter( 'wp_nav_menu_items', 'my_account_loginout_link', 10, 2 );
/**
* Add WooCommerce My Account Login/Logout to Menu
*
* @see https://support.woothemes.com/hc/en-us/articles/203106357-Add-Login-Logout-Links-To-The-Custom-Primary-Menu-Area
*/
function my_account_loginout_link( $items, $args ) {
if (is_user_logged_in() && $args->theme_location == 'primary') { //change your theme location menu to suit
$items .= '<li><a href="'. wp_logout_url( get_permalink( wc_get_page_id( 'shop' ) ) ) .'">Log Out</a></li>'; //change logout link, here it goes to 'shop', you may want to put it to 'myaccount'
}
elseif (!is_user_logged_in() && $args->theme_location == 'primary') {//change your theme location menu to suit
$items .= '<li><a href="' . get_permalink( wc_get_page_id( 'myaccount' ) ) . '">Log In</a></li>';
}
return $items;
}
view raw woologin.php hosted with ❤ by GitHub

It differs from the referred article as that adds the WordPress default login/logout page.

ref

3 Comments

  1. Bri on October 20, 2016 at 6:03 pm

    Great snippet thank you.

    Quick question, if I have ‘My Account’ already defined as part of the primary menu then how can I add Login/Logout to that as a sub-menu?

  2. Evan on April 29, 2016 at 12:38 pm

    Any idea why this php snippet is putting the link below the existing secondary menu and not adding it too the three other menu items? Running Divine Theme modified heavily; but core menu structure is default.

Leave a Comment





%d bloggers like this: