61
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!
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://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:
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://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;
}