Add login/logout to menu Woocommerce WordPress

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>

Related posts

Leave a Reply

4 comments

  1. 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!

  2. There are many codes that doesn’t work. I just found one that works perfectly. Start functions.php with:

    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 == 'top') { //change your theme location menu to suit
            $items .= '<li><a class="nav-link" href="'. wp_logout_url( get_permalink( wc_get_page_id( 'shop' ) ) ) .'">Sair</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 == 'top') {//change your theme location menu to suit
            $items .= '<li><a class="nav-link" href="' . get_permalink( wc_get_page_id( 'myaccount' ) ) . '">Entrar</a></li>';
        }
        return $items;
    }
    
  3. Easier way is to change the menu structure

    Login (drop down menu)

    • Lost Password

    My Account (drop down menu)

    • Change Password
    • Lost password
    • Logout

    then show or hide following page ID in woocommerce-functions.php (:123)

    function woocommerce_nav_menu_items( $items, $args ) {
        if ( ! is_user_logged_in() ) {
    
            $hide_pages   = array();
            $hide_pages[] = 20;
            $hide_pages   = apply_filters( 'woocommerce_logged_out_hidden_page_ids', $hide_pages );
    
            foreach ( $items as $key => $item ) {
                if ( ! empty( $item->object_id ) && ! empty( $item->object ) && in_array( $item->object_id, $hide_pages ) && $item->object == 'page' ) {
                    unset( $items[ $key ] );
                }
            }
        } else {
            $hide_pages   = array();
            $hide_pages[] = 18;
            $hide_pages   = apply_filters( 'woocommerce_logged_out_hidden_page_ids', $hide_pages );
    
            foreach ( $items as $key => $item ) {
                if ( ! empty( $item->object_id ) && ! empty( $item->object ) && in_array( $item->object_id, $hide_pages ) && $item->object == 'page' ) {
                    unset( $items[ $key ] );
                }
            }
        }
    
        return $items;
    }
    

    my login page id was ’20’, and my account page id was ’18’

    Hope it helps anyone in need