Skip to content

Instantly share code, notes, and snippets.

@webdados
Last active February 28, 2018 10:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webdados/91ca20ecfa232a89cbd59321a16c5d27 to your computer and use it in GitHub Desktop.
Save webdados/91ca20ecfa232a89cbd59321a16c5d27 to your computer and use it in GitHub Desktop.
Change WooCommerce "My Account" item menu title when the user is logged in
<?php
/* If you want to limit it to a specific theme location */
add_filter( 'nav_menu_item_title', 'custom_nav_menu_item_title', 10, 3 );
function custom_nav_menu_item_title( $title, $item, $args ) {
//Menu item should be set with the text you want to use for "logged out" users
//Logged in?
if ( is_user_logged_in() ) {
//Correct menu location
$theme_locations = array( 'acc-menu', 'mobile-secondary-menu' ); //Update to your own locations here
if ( isset( $args->theme_location ) && in_array( $args->theme_location, $theme_locations ) ) {
//Is it a page?
if ( isset( $item->object ) && $item->object == 'page' ) {
//Is it the correct page?
$id_page = get_option( 'woocommerce_myaccount_page_id' ); //Or the page ID if it's not a WooCommerce install
if ( isset( $item->object_id ) && intval( $item->object_id ) == intval( $id_page ) ) {
//Get the current user
$user = wp_get_current_user();
$title = 'Hello '.$user->display_name; //Or any other "logged in" text
}
}
}
}
return $title;
}
/* Simpler version - Not checking the theme location */
add_filter( 'nav_menu_item_title', 'custom_nav_menu_item_title', 10, 3 );
function custom_nav_menu_item_title( $title, $item, $args ) {
//Menu item should be set with the text you want to use for "logged out" users
//Logged in?
if ( is_user_logged_in() ) {
//What page?
$id_page = get_option( 'woocommerce_myaccount_page_id' ); //Or the page ID if it's not a WooCommerce install
//Is it the correct page?
if ( isset( $item->object_id ) && intval( $item->object_id ) == intval( $id_page ) ) {
//Get the current user
$user = wp_get_current_user();
$title = 'Hello '.$user->display_name; //Or any other "logged in" text
}
}
return $title;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment