Ever wanted to display a my account page link in your WooCommerce theme files, and to have this changed to “Login / Register” in case your user is not logged in, dynamically?
Well the snippet below, when added into the relevant location in your theme template files will do just that!
We tend to wrap it in other controls, or include button links, but this makes a nice addition to header / footer links in a theme which is for WooCommerce to help users navigate to their My Account page(s).
The Code – Show “My Account” to logged in users and “Register / Login” to Guests
Note: Gist (above) updated 23 Nov 2018.
What it does
The code above will insert a “My Account” link for logged in users, or a “Register / Login” link to guests. As simple as that. It can even be used in menus.
Leave a comment
If you have questions or comments about this snippet, please leave them below. The aim, here, is to build a useful resource for WooCommerce developers around the globe.
Hi Robin, this is excellent. What is needed to add a ‘Log Out’ link following account.
So the logged-in user’s line is: Account | Log Out
The guest’s user’s line is: Log In | Register
Many thanks,
Hi Kevin,
Should be do-able using something like:
wp_logout_url( get_permalink( wc_get_page_id( 'myaccount' ) ) )
For the link itself. The logic behind it will be similar to the other link.
I’ll put this on the list to make a quick tutorial… this can take a while though 🙂
Kindly tell me where to put this code snippet in. I am trying to put it in functions.php but its giving a syntax error that unexpected < at the end of the file
Hi, Bilal, from the way the code is formatted, I suppose it should go into your html, where you want the links to show.