public
Last active

WooCommerce add login/logout buttons to wordpress menu

  • Download Gist
gistfile1.php
PHP
1 2 3 4 5 6 7 8 9 10
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;
}

use wc_get_page_id(), because woocommerce_get_page_id() deprecated

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

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

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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.