Home » WooCommerce: Rename “My Account” If Logged Out @ Nav Menu

WooCommerce: Rename “My Account” If Logged Out @ Nav Menu

by Tutor Aspire

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 tutoraspire.com FREE
 * @author        Tutor Aspire
 * @compatible    WooCommerce 4.5
 * @donate $9     https://www.tutoraspire.com
 */

add_filter( 'wp_nav_menu_items', 'tutoraspire_dynamic_menu_item_label', 9999, 2 ); 

function tutoraspire_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 tutoraspire.com FREE
 * @author        Tutor Aspire
 * @compatible    WooCommerce 4.5
 * @donate $9     https://www.tutoraspire.com
 */

add_filter( 'wp_nav_menu_items', 'tutoraspire_dynamic_menu_item_label', 9999, 2 ); 

function tutoraspire_dynamic_menu_item_label( $items, $args ) { 
if ( ! is_user_logged_in() ) { 
$items = str_replace( "Account", "Login", $items ); 
} 
return $items; 
} 

You may also like