To display the current user’s username when they are logged into a WordPress site, you can use the wp_get_current_user() function.
Here’s an example of how to use this function:
<?php
$current_user = wp_get_current_user(); // Get the current user object
if ($current_user->ID > 0) { // Check if the user is logged in
echo 'Welcome, ' . esc_html($current_user->user_login) . '!'; // Display the username
} else {
echo 'Welcome, Guest!';
}
?>
Place this code snippet in the appropriate theme template file where you want to display the user’s username, such as header.php or sidebar.php.
This code will display “Welcome, [username]!” if the user is logged in and “Welcome, Guest!” if the user is not logged in.
Remember to always backup your files before making any changes, and test your changes on a staging site or local development environment before applying them to your live site.