The WordPress admin bar, by default, greets you with a friendly “Howdy” followed by your username. While this greeting is perfectly cordial, you might want to personalize it further by changing it based on the time of day. This can add a nice touch of professionalism and a bit of fun to your WordPress dashboard. In this article, we will guide you through the steps to achieve this.
Step 1: Access Your Theme’s Functions.php File
To start, you’ll need to access your theme’s functions.php file. This file is where you’ll add the custom code to change the greeting text.
- Log in to your WordPress dashboard.
- Navigate to
Appearance > Theme Editor. - On the right-hand side, find and click on
functions.php.
Note: Always make a backup of your functions.php file before making any changes. This will help you restore the original file if something goes wrong.
Step 2: Add the Custom Function
Next, you’ll add a custom function that determines the appropriate greeting based on the time of day. Here’s the code you need to add to your functions.php file:
function custom_admin_bar_greeting($wp_admin_bar) {
$current_user = wp_get_current_user();
$hour = date('H');
if ($hour >= 5 && $hour = 12 && $hour = 17 && $hour get_node('my-account');
$newtitle = str_replace('Howdy,', $greeting . ',', $my_account->title);
$wp_admin_bar->add_node([
'id' => 'my-account',
'title' => $newtitle,
]);
}
add_action('admin_bar_menu', 'custom_admin_bar_greeting', 25);
This code works as follows:
- Get Current User: It retrieves the current user’s information.
- Determine Time of Day: It gets the current hour and determines the appropriate greeting.
- Modify Admin Bar Greeting: It replaces the default “Howdy” text with the new greeting based on the time of day.
Step 3: Save and Test
After adding the code, save your functions.php file and refresh your WordPress admin dashboard. You should see the greeting in the admin bar change according to the time of day.
Additional Tips
- Error Handling: If you encounter any errors, check the syntax and ensure that the code is correctly placed in the
functions.phpfile. - Customization: Feel free to customize the greetings or the time ranges to suit your preferences.
- Backup: Always maintain a recent backup of your website before making any changes to the code.
By following these steps, you can create a dynamic and personalized greeting in your WordPress admin bar, enhancing the user experience. Happy coding!
Need custom WordPress development or ongoing site maintenance? I build and maintain WordPress sites for businesses and agencies — see my web development services.
On this page
0%
Perlat Kociaj
Full Stack Web Developer