WooCommerce: Rename “My Account” If Logged Out @ Nav Menu
You could install a free WordPress plugin to enable “LOGIN / LOGOUT” menu links – or you could use a super simple snippet. Once again, the less plugins you use the better, especially if you can replace them with a few lines of code.
In this case study, I have added the “My Account” page to the navigation menu and I want that label to change to “Login” if the user is logged out. Enjoy!
When logged out, my own website navigation menu shows a “LOGIN” button. If logged in, the same menu link label changes to “MY ACCOUNT”.
PHP Snippet: Conditionally Rename “My Account” Menu Label If User Is Logged Out
/**
* @snippet Rename "My Account" Link @ WooCommerce/WP Nav Menu
* @how-to Get CustomizeWoo.com FREE
* @author Rodolfo Melogli
* @compatible WooCommerce 4.5
* @donate $9 https://businessbloomer.com/bloomer-armada/
*/
add_filter( 'wp_nav_menu_items', 'bbloomer_dynamic_menu_item_label', 9999, 2 );
function bbloomer_dynamic_menu_item_label( $items, $args ) {
if ( ! is_user_logged_in() ) {
$items = str_replace( "My Account", "Login", $items );
}
return $items;
}
Please note that if you rename the “navigation label” you must change the code accordingly:
Pay attention if the navigation label is customized – you’d need to adapt the snippet!
PHP Snippet: Conditionally Rename Custom Menu Label If User Is Logged Out
/**
* @snippet Rename Custom Link Label @ WooCommerce/WP Nav Menu
* @how-to Get CustomizeWoo.com FREE
* @author Rodolfo Melogli
* @compatible WooCommerce 4.5
* @donate $9 https://businessbloomer.com/bloomer-armada/
*/
add_filter( 'wp_nav_menu_items', 'bbloomer_dynamic_menu_item_label', 9999, 2 );
function bbloomer_dynamic_menu_item_label( $items, $args ) {
if ( ! is_user_logged_in() ) {
$items = str_replace( "Account", "Login", $items );
}
return $items;
}
Please let me know in the comments if everything went as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting.
If you think this code saved you time & money, feel free to join 17,000+ WooCommerce Weekly subscribers for blog post updates and 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance!
Need Help with WooCommerce?
Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!
Rodolfo Melogli
Business Bloomer Founder
Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.
Thank you SO much! Worked beautifully in both, the primary and secondary menu. Of course, this was after I made sure everything was actually named THE SAME in both menus.
I used the exact same snippet as you suggested, but it seem to only work on the /my-account/ page and not on other pages such as the home page. Any idea on how I can resolve this?
Hey, as of March 13 2021 this code, and actually all of the code I used from you doesnt work. The main reason is because of woocommerce’s 5.0.0 (or it could be 5.1.0, I’m no really sure which one is) update. Can you help everyone fixing this issue please?
ALL of my snippets don’t work? LOL, that’s pretty bad. Woo 5.0 wasn’t that big deal, so I’d be surprised. To troubleshoot, disable all plugins but WooCommerce and also switch temporarily to “Twentytwenty” theme (load the snippet there in functions.php) as explained here: https://www.businessbloomer.com/lesson/trwm4l01/
Once you do that, does it work? If yes, you have a problem with your current theme or one of the plugins.
Hi! Works fine on my website. Thank you for the tutorial. I just wanted to ask: how can I change the link of the login page? I have my own customized login page and I want to change it from the wordpress default page.
I tried this on two different websites, they’re identical clones of one another, the only difference is that the one is on a higher version of php 7.4… and it’s not working
Hi Morgan, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
I just tried this, the exact snippet, but it’s not working for me.
Other snippets I found here are working perfectly. Any idea what the problem could be?
It works flawlessly. Check the labels, if it is “My Account” or not. Those who are confused about label, it is the Page name or rather the Menu item name. It is case sensitive, if your account page is named something else, try using that instead of “My Account” in the snippet code. Stop blind copy paste, use your head guys.
Hi Rodolfo,
I have the same problem. Did exactly as you teaching in your explanation video and used your exact snippet.
I use current version of WooCommerce: 3.8.0, and WP version is 5.3
Questions? Feedback? Support? Leave your Comment Now! _____
If you are writing code, please wrap it between shortcodes: [php]code_here[/php]. Failure to complying with this (as well as going off topic, not writing in English, etc.) will result in comment deletion. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining BloomerArmada to get blog comment reply priority, ask me 1-to-1 WooCommerce questions and enjoy many more perks. Thank you :)
With 100,000 (and growing) monthly organic sessions, Business Bloomer is the most consistent, most active and most complete WooCommerce development/customization blog.
Of course this website itself uses the WooCommerce plugin, the Storefront theme and runs on a WooCommerce-friendly hosting.
Thank you SO much! Worked beautifully in both, the primary and secondary menu. Of course, this was after I made sure everything was actually named THE SAME in both menus.
Nice!
How would you do this if you wanted it to say login is logged out ANS logout if logged in?
Not sure I follow?
Works great with woocommerce 6.2.0. Thanks for your enormous work Rodolfo!
Great!
Thanks for this, how would this need to be modified to work on a secondary menu (I’m using Divi).
Hi Chris! This should work on any WP menu, secondary included
Dear,
I used the exact same snippet as you suggested, but it seem to only work on the /my-account/ page and not on other pages such as the home page. Any idea on how I can resolve this?
Thank you in advance.
Regards,
Mathias
Hi Mathias, I use this on my own website. This is meant to work in your Navigation Menu
Hey, as of March 13 2021 this code, and actually all of the code I used from you doesnt work. The main reason is because of woocommerce’s 5.0.0 (or it could be 5.1.0, I’m no really sure which one is) update. Can you help everyone fixing this issue please?
ALL of my snippets don’t work? LOL, that’s pretty bad. Woo 5.0 wasn’t that big deal, so I’d be surprised. To troubleshoot, disable all plugins but WooCommerce and also switch temporarily to “Twentytwenty” theme (load the snippet there in functions.php) as explained here: https://www.businessbloomer.com/lesson/trwm4l01/
Once you do that, does it work? If yes, you have a problem with your current theme or one of the plugins.
Hope this helps!
Hi! Works fine on my website. Thank you for the tutorial. I just wanted to ask: how can I change the link of the login page? I have my own customized login page and I want to change it from the wordpress default page.
Hi, maybe from the WordPress menu?
Hi, the snippet works great. But does it also work on the My Account on the right side of the menu? The one that comes with the plugin?
No sorry this is for WordPress menus
Worked a treat for me (using PHP 7.4) .
Thank you!
Cool
I tried this on two different websites, they’re identical clones of one another, the only difference is that the one is on a higher version of php 7.4… and it’s not working
This is the error I get:
“The snippet has been deactivated due to an error on line 4:
Cannot redeclare function dynamic_label_change. “
Not sure, that’s not my function
Just tried it and it works great! No need for a plugin. Thank you very much!
Way to go!
Hello,
your code still work smoothly
do you have a tutorial ifsomeone login then the teks link will change to the name of the user instead of my-account
thank you
Hi Morgan, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
I just tried this, the exact snippet, but it’s not working for me.
Other snippets I found here are working perfectly. Any idea what the problem could be?
Read through the comments. This is case-sensitive, maybe https://businessbloomer.com/woocommerce-rename-my-account-menu-label-if-logged-out/#comment-355148 might help
It works flawlessly. Check the labels, if it is “My Account” or not. Those who are confused about label, it is the Page name or rather the Menu item name. It is case sensitive, if your account page is named something else, try using that instead of “My Account” in the snippet code. Stop blind copy paste, use your head guys.
TOP!
it didn’t work 🙁 . i follow every step on your tutorial.
This is case sensitive – try again?
Awesome snippet – worked perfectly (once I’d changed it from ‘My account’ to ‘My Account’ 😉 )
Thank you!
Great!
Hi Rodolfo,
I have the same problem. Did exactly as you teaching in your explanation video and used your exact snippet.
I use current version of WooCommerce: 3.8.0, and WP version is 5.3
Hi Sagi. Do you have a “My Account” navigation menu link (because I do and the snippet works on this same website)?
Hi, just tried it out and nope, it didn’t work.
Did you use my exact snippet?