Silicon Dales
  • About
  • Magazine
  • Services
  • Tutorials
  • ⚡ Optimize
    • WooCommerce Speedup
    • WordPress Speedup
  • Contact Us
  • About
  • Magazine
  • Services
  • Tutorials
  • ⚡ Optimize
    • WooCommerce Speedup
    • WordPress Speedup
  • Contact Us
Home / Tutorials / WooCommerce Tutorials / How to display WooCommerce my account page link to logged in users – or login / register link if not logged in

How to display WooCommerce my account page link to logged in users – or login / register link if not logged in

Posted on December 19, 2017February 7, 2019 by Robin Scott — 4 Comments
Category: WooCommerce Tutorials
Tags: Gist, Login, My Account, Register, WooCommerce, WordPress

Table of Contents

  • The Code – Show “My Account” to logged in users and “Register / Login” to Guests
  • What it does
  • Leave a comment

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

// Added by Robin Scott of Silicon Dales here to demonstrate My Account / login links: https://silicondales.com/tutorials/woocommerce/display-woocommerce-account-page-link-logged-users-login-register-link-not-logged/
<?php if ( is_user_logged_in() ) { ?>
<a href="<?php echo get_permalink( get_option('woocommerce_myaccount_page_id') ); ?>" title="<?php _e('My Account','woocommerce'); ?>"><?php _e('My Account','woocommerce'); ?></a>
<?php }
else { ?>
<a href="<?php echo get_permalink( get_option('woocommerce_myaccount_page_id') ); ?>" title="<?php _e('Login / Register','woocommerce'); ?>"><?php _e('Login / Register','woocommerce'); ?></a>
<?php } ?>
view raw gistfile1.txt hosted with ❤ by GitHub

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.

Post navigation

Previous post: WooCommerce Tutorial: Allow users only to purchase items once
Next post: Code to hide content on the front page of WordPress

4 thoughts on “How to display WooCommerce my account page link to logged in users – or login / register link if not logged in”

  1. Kevin Price
    January 30, 2019

    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,

    Reply
    1. Robin Scott
      January 30, 2019

      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 🙂

      Reply
  2. Bilal Arshad
    April 1, 2020

    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

    Reply
    1. Pedro Frota
      April 17, 2020

      Hi, Bilal, from the way the code is formatted, I suppose it should go into your html, where you want the links to show.

      Reply
Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

About Silicon Dales

Silicon Dales are accredited WooCommerce developers and WordPress experts working remotely from an HQ in Manchester, UK.

Registered Office: Silicon Dales Ltd, Bloc, 17 Marble Street, Manchester, M2 3AW.

See more about us here.

Contact Us
  • Tel: +44 (0)161 870 2536
  • Email: Getintouch@silicondales.com

Registered in England & Wales. Company number: 7324510. Directors: Robin J.E. Scott, Jonathan M.G. Farrington and Linda Scott. VAT Number GB 111 682 442.

Click here to contact us.

Quick Links
  • About Us
  • Contact Us
  • Glossary
  • Legal
  • Opportunities
  • Shop for Computer Components
© 2019 Silicon Dales Ltd
Coupon Code

Coupon Code: BFCM18

Offer:
30% off for 3 Months on all hosting plans!
Start Date:
21-11-2018
End Date:
30-11-2018
Halloween Offer

[vc_row][vc_column width=”1/2″][vc_single_image image=”63236″ img_size=”full” alignment=”center”][vc_column_text]Enter your details and click the button to the right to take advantage of this incredible offer![/vc_column_text][/vc_column][vc_column width=”1/2″][vc_column_text]

[/vc_column_text][/vc_column][/vc_row]