How to replace the top bar menu label with the logged-in user’s name in WordPress
This is an excellent way to add a dynamic, personal touch to your site. The most robust PHP-only solution is to use a filter that changes the menu item’s title and link based on a specific CSS class.
This method is better than str_replace() because it doesn’t break if you rename the menu item, and it uses the correct WordPress “filters” for the job.
Here is the complete step-by-step guide.
Step 1: Add a “Placeholder” Menu Item
First, you need to add a special item to your menu that our PHP code can find and replace.
- Go to Appearance > Menus in your WordPress dashboard.
- At the top right of the screen, click “Screen Options”. Check the box for “CSS Classes” if it’s not already checked.
- On the left, find the “Custom Links” panel.
- URL: Type
#(this is just a placeholder). - Link Text: Type
Account(this will be replaced). - Click “Add to Menu”.
- URL: Type
- Find the new “Account” item in your menu structure and click the small arrow to open its options.
- In the “CSS Classes” field, type a unique name, for example:
dynamic-user-menu - Click “Save Menu”.
Step 2: Add the PHP Code
Now, we’ll add PHP code that finds that dynamic-user-menu item and changes its text and link.
- Go to Appearance > Theme File Editor.
- On the right, select your Theme Functions (functions.php) file.
(Warning: Always use a Child Theme for this. If you edit your main theme’s file, your changes will be deleted when the theme updates.) - Paste the following code at the very bottom of the file:
/**
* Dynamically change a menu item's title based on login status.
* Looks for a menu item with the CSS class 'dynamic-user-menu'.
*/
add_filter( 'nav_menu_item_title', 'my_dynamic_user_menu_title', 10, 2 );
function my_dynamic_user_menu_title( $title, $item ) {
// Check if this menu item has our special class
if ( in_array( 'dynamic-user-menu', $item->classes ) ) {
if ( is_user_logged_in() ) {
// User is logged in: Get their name
$user = wp_get_current_user();
$title = $user->display_name; // Or $user->first_name
} else {
// User is logged out: Set text to "Sign-in"
$title = 'Sign-in';
}
}
return $title;
}
/**
* Dynamically change a menu item's URL based on login status.
* Looks for a menu item with the CSS class 'dynamic-user-menu'.
*/
add_filter( 'nav_menu_link_attributes', 'my_dynamic_user_menu_url', 10, 2 );
function my_dynamic_user_menu_url( $atts, $item ) {
// Check if this menu item has our special class
if ( in_array( 'dynamic-user-menu', $item->classes ) ) {
if ( is_user_logged_in() ) {
// User is logged in: Link to their profile
$atts['href'] = get_edit_user_link();
} else {
// User is logged out: Link to the login page
$atts['href'] = wp_login_url( get_permalink() ); // Redirects back to the current page
}
}
return $atts;
}
- Click “Update File”.
What This Code Does:
This code snippet adds two filters:
my_dynamic_user_menu_title: This function finds any menu item with thedynamic-user-menuclass.- If the user is logged in, it replaces the title with their “Display name.”
- If the user is logged out, it replaces the title with “Sign-in.”
my_dynamic_user_menu_url: This function does the same thing for the link (URL).- If the user is logged in, it links to their “Edit Profile” page.
- If the user is logged out, it links to the WordPress login page.
How to add the CSS class dynamic-user-menu to a placeholder menu item (the one you named “Account”)?
Here are the step-by-step instructions. The most important part is enabling the “CSS Classes” field, as it is hidden by default.
Step 1: Go to the Menus Page
In your WordPress dashboard, navigate to Appearance > Menus.
Step 2: Enable the CSS Classes Field
- At the very top right of the Menus page, click the Screen Options tab.
- A panel will drop down. Find the checkbox labeled CSS Classes and check it.
Step 3: Find and Edit Your Menu Item
- In your menu structure below, find the “Account” placeholder item you created.
- Click the small arrow on the right side of the item to open its settings.
Step 4: Add the CSS Class
- You will now see a new field labeled CSS Classes (optional).
- Type
dynamic-user-menudirectly into this field. - Click the blue Save Menu button.
Step 5: Add the CSS Code
/* Make the link bold and change its color */
.dynamic-user-menu a .nav-drop-title-wrap {
font-weight: bold;
color: green; !important;
}
How to debug the $user in my_dynamic_user_menu_title()
The best and safest way to debug a variable like $user is to log its content to a file, rather than printing it to the screen (which would break your menu).
Here is the recommended method using WordPress’s built-in debug log.
How to Debug with WP_DEBUG_LOG (Recommended)
This method prints the variable’s contents to a private file on your server, so it doesn’t affect your live site.
Step 1: Enable the Debug Log
- Using an FTP client or your web host’s file manager, open the
wp-config.phpfile, which is in the main folder of your WordPress installation. - Find the line that says
/* That's all, stop editing! Happy publishing. */. - Before that line, add the following code:
define( 'WP_DEBUG', true ); define( 'WP_DEBUG_LOG', true ); define( 'WP_DEBUG_DISPLAY', false );
WP_DEBUG_LOGtells WordPress to save errors to a file.WP_DEBUG_DISPLAY(set tofalse) ensures errors don’t print to the screen and break your site.
Step 2: Modify Your functions.php Code
Now, edit your my_dynamic_user_menu_title() function to include the error_log() function. This will write the contents of the $user object to the log file.
function my_dynamic_user_menu_title( $title, $item ) {
// Check if this menu item has our special class
if ( in_array( 'dynamic-user-menu', $item->classes ) ) {
if ( is_user_logged_in() ) {
$user = wp_get_current_user();
// --- THIS IS THE DEBUGGING LINE ---
error_log( 'my_dynamic_user_menu_title User Object: ' . print_r( $user, true ) );
// ----------------------------------
$title = $user->display_name;
} else {
$title = 'Sign-in';
}
}
return $title;
}
Step 3: Check the Log File
- Save your
functions.phpfile. - Go to your website in your browser and reload the page (while you are logged in). This will trigger the code.
- On your server, look inside the
/wp-content/folder. - You will find a new file named
debug.log. Open it. - You will see the full contents of the
$userobject, which will look something like this:
[02-Nov-2025 13:02:26 UTC] my_dynamic_user_menu_title User Object: WP_User Object
(
[data] => stdClass Object
(
[ID] => 1
[user_login] => admin
[user_pass] => ...
[user_nicename] => admin
[user_email] => admin@example.com
[user_url] =>
[user_registered] => 2024-10-20 00:00:00
[user_activation_key] =>
[user_status] => 0
[display_name] => Your Admin Name
)
[ID] => 1
[caps] => Array
(
[administrator] => 1
)
[cap_key] => wp_capabilities
[roles] => Array
(
[0] => administrator
)
[allcaps] => Array
(
[administrator] => 1
...
)
...
)
Step 4: Clean Up
Once you are finished debugging:
- Remove the
error_log(...)line from yourfunctions.phpfile. - Change
define( 'WP_DEBUG', true );todefine( 'WP_DEBUG', false );in yourwp-config.phpfile.
What You Are Probably Looking For
You are likely debugging this because $user->display_name isn’t showing the name you want. The debug log will show you all the available properties.
You can change the name by replacing $user->display_name with one of these other common properties:
$user->user_login: The user’s unchangeable username (e.g., “admin”).$user->first_name: The user’s “First Name” from their profile.$user->last_name: The user’s “Last Name” from their profile.$user->nickname: The user’s “Nickname” from their profile.
Example Fix: To show the user’s first name instead, you would simply change the line in your function to:
$title = $user->first_name;
