Create a gist now

Instantly share code, notes, and snippets.

Embed
WooCommerce add login/logout buttons to wordpress menu
add_filter( 'wp_nav_menu_items', 'add_loginout_link', 10, 2 );
function add_loginout_link( $items, $args ) {
if (is_user_logged_in() && $args->theme_location == 'primary') {
$items .= '<li><a href="'. wp_logout_url( get_permalink( woocommerce_get_page_id( 'myaccount' ) ) ) .'">Log Out</a></li>';
}
elseif (!is_user_logged_in() && $args->theme_location == 'primary') {
$items .= '<li><a href="' . get_permalink( woocommerce_get_page_id( 'myaccount' ) ) . '">Log In</a></li>';
}
return $items;
}
@klihelp

This comment has been minimized.

Show comment
Hide comment
@klihelp

klihelp Mar 2, 2014

use wc_get_page_id(), because woocommerce_get_page_id() deprecated

klihelp commented Mar 2, 2014

use wc_get_page_id(), because woocommerce_get_page_id() deprecated

@cinghaman

This comment has been minimized.

Show comment
Hide comment
@cinghaman

cinghaman Mar 5, 2014

Where exactly to add this function - becoz i added it into functions file and it says wc_get_page_id function not defined

Where exactly to add this function - becoz i added it into functions file and it says wc_get_page_id function not defined

@cinghaman

This comment has been minimized.

Show comment
Hide comment
@cinghaman

cinghaman Mar 5, 2014

ok i think i am using older version of woo it works thanks

ok i think i am using older version of woo it works thanks

@thinkluke

This comment has been minimized.

Show comment
Hide comment
@thinkluke

thinkluke Mar 25, 2014

I just use:
Logout Link: /?customer-logout=true
Login: /my-account/

I just use:
Logout Link: /?customer-logout=true
Login: /my-account/

@ajit1

This comment has been minimized.

Show comment
Hide comment
@ajit1

ajit1 Jan 30, 2015

Thanks, more filtered
Add to functions.php
add_filter( 'wp_nav_menu_items', 'add_loginout_link', 10, 2 );
function add_loginout_link( $items, $args ) {
if (is_user_logged_in()) {
$items .= '

  • Log out
  • ';
    }
    elseif (!is_user_logged_in()) {
    $items .= '
  • Log in
  • ';
    }
    return $items;
    }

    ajit1 commented Jan 30, 2015

    Thanks, more filtered
    Add to functions.php
    add_filter( 'wp_nav_menu_items', 'add_loginout_link', 10, 2 );
    function add_loginout_link( $items, $args ) {
    if (is_user_logged_in()) {
    $items .= '

  • Log out
  • ';
    }
    elseif (!is_user_logged_in()) {
    $items .= '
  • Log in
  • ';
    }
    return $items;
    }

    @joekender

    This comment has been minimized.

    Show comment
    Hide comment
    @joekender

    joekender Jan 31, 2015

    Hi ait1,

    Can you help me?

    In my theme (replay theme (themeforest)) my login logout menu for woocomerce dont work... :(::(

    Hi ait1,

    Can you help me?

    In my theme (replay theme (themeforest)) my login logout menu for woocomerce dont work... :(::(

    @ZCweb

    This comment has been minimized.

    Show comment
    Hide comment
    @ZCweb

    ZCweb Aug 31, 2017

    I'd like the Login and logout to go in a dropdown menu. Not to be added on to the menu as the last list item. How can I control where it goes? My last list item is 'My Account' and I want it under there as a dropdown.

    ZCweb commented Aug 31, 2017

    I'd like the Login and logout to go in a dropdown menu. Not to be added on to the menu as the last list item. How can I control where it goes? My last list item is 'My Account' and I want it under there as a dropdown.

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment