I am trying to figure out how to add a login/logout to the menu. When I add this code to the wordpress header the content and sidebar disappear. How can I add the login/logout to the menu without losing the rest of my page. I have tried adding it in the settings menu and it doesn’t work with the theme I’m using.
<ul>
<?php
$myaccount_page_id = get_option( 'woocommerce_myaccount_page_id' );
if ( $myaccount_page_id  && !is_user_logged_in()) {
$myaccount_page_url = get_permalink( $myaccount_page_id );
?>
<li><a href="<?php echo $myaccount_page_url; ?>" class="login-header"><?php _e('Login', 'woocommerce'); ?></a></li>
<?php
}
$myaccount_page_id = get_option( 'woocommerce_myaccount_page_id' );
if ( $myaccount_page_id && is_user_logged_in()) {
$logout_url = wp_logout_url( get_permalink( $myaccount_page_id ) );
if ( get_option( 'woocommerce_force_ssl_checkout' ) == 'yes' )
$logout_url = str_replace( 'http:', 'https:', $logout_url );
?>
<li><a href="<?php echo $logout_url; ?>" class="login-header"><?php _e('Logout', 'woocommerce'); ?></a></li>
<?php } ?>
<li><a href="<?php echo $woocommerce->cart->get_cart_url(); ?>" class="cart-header"><?php _e('Shopping Cart', 'woocommerce'); ?> <?php echo sprintf(_n('(%d)', '(%d)', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?></a></li>
<li><a href="<?php echo $woocommerce->cart->get_checkout_url(); ?>" class="check-header"><?php _e('Checkout', 'woocommerce'); ?></a></li>
</ul>
Logout Link:
/?customer-logout=true
Login link sent to My Account page to trigger login:
/my-account/
If you use a plugin like Theme My Login you can just create the link to the login page in your menu. It will display “Login” if the person is not logged in, and “Log Out” if the person is logged in. Hope this helps!
There are many codes that doesn’t work. I just found one that works perfectly. Start functions.php with:
Easier way is to change the menu structure
Login (drop down menu)
My Account (drop down menu)
then show or hide following page ID in woocommerce-functions.php (:123)
my login page id was ’20’, and my account page id was ’18’
Hope it helps anyone in need