How to add Logout link to WordPress WooCommerce

First, find the text that says something like Login or register. A tool like AstroGrep, if you’re on Windows, may be of great help if you don’t know in which file you have to look (like I didn’t).

Then you will most probably see something like this:

<div class="account_links">
	<?php if ( is_user_logged_in() ) : ?>
		<a href="<?php echo get_permalink( get_option('woocommerce_myaccount_page_id') ); ?>" title="<?php _e('My Account','wpdance'); ?>">Mein Konto</a>
	<?php else : ?>
		<a href="<?php echo get_permalink( get_option('woocommerce_myaccount_page_id') ); ?>" title="<?php _e('Login','wpdance'); ?>">Anmelden</a>
		<span>oder</span>
		<a href="<?php echo get_permalink( get_option('woocommerce_myaccount_page_id') ); ?>" title="<?php _e('Create An Account','wpdance'); ?>">registrieren</a>
	<?php endif; ?>
</div>

In the is_user_logged_in() part of the if add the following statement:

<a href="<?php echo wp_logout_url( get_permalink( woocommerce_get_page_id( 'myaccount' ) ) );?>">Logout</a>

so that the final code then looks like this:

<div class="account_links">
	<?php if ( is_user_logged_in() ) : ?>
		<a href="<?php echo get_permalink( get_option('woocommerce_myaccount_page_id') ); ?>" title="<?php _e('My Account','wpdance'); ?>">Mein Konto</a>
		<span> | </span>
		<a href="<?php echo wp_logout_url( get_permalink( woocommerce_get_page_id( 'myaccount' ) ) );?>">Logout</a>
	<?php else : ?>
		<a href="<?php echo get_permalink( get_option('woocommerce_myaccount_page_id') ); ?>" title="<?php _e('Login','wpdance'); ?>">Anmelden</a>
		<span>oder</span>
		<a href="<?php echo get_permalink( get_option('woocommerce_myaccount_page_id') ); ?>" title="<?php _e('Create An Account','wpdance'); ?>">registrieren</a>
	<?php endif; ?>
</div>

This is it for this quick tip, hope it helps someone. I’ll keep it for reference for when I can’t remember it straight away…

Written by Nikola Brežnjak