Skip to content

Instantly share code, notes, and snippets.

@norcross
Created February 26, 2025 14:44
Show Gist options
  • Save norcross/1785668169100480123198a2fe7feb9c to your computer and use it in GitHub Desktop.
Save norcross/1785668169100480123198a2fe7feb9c to your computer and use it in GitHub Desktop.
update intro text description on the admin bar
<?php
add_action( 'admin_bar_menu', 'myprefix_filter_profile_intro_text', 9992 );
/**
* Modify the "Howdy" to something an adult would say.
*
* @param WP_Admin_Bar $wp_admin_bar The global WP_Admin_Bar object.
*
* @return void.
*/
function myprefix_filter_profile_intro_text( $wp_admin_bar ) {
// Get the account link.
$my_account = $wp_admin_bar->get_node( 'my-account' );
// Bail if this isn't around.
if ( empty( $my_account ) || ! is_object( $my_account ) || empty( $my_account->title ) ) {
return;
}
// If "Howdy" isn't there, someone else modded it, so don't try.
if ( ! str_contains( $my_account->title, 'Howdy' ) ) {
return;
}
// Begin by stripping the "Howdy" part.
$base_title = str_replace( 'Howdy,', '', $my_account->title );
$base_title = ltrim( $base_title );
// Now get the current hour.
$current_hr = current_time( 'G', false );
// Set our new prefix.
$new_prefix = __( 'Hello', 'my-text-domain' );
// Now do some building.
switch ( $current_hr ) {
// Handle late evening.
case $current_hr < 3:
$new_prefix = __( 'Good Evening', 'my-text-domain' );
break;
// Handle morning.
case $current_hr >= 3 && $current_hr < 12:
$new_prefix = __( 'Good Morning', 'my-text-domain' );
break;
// Handle afternoon.
case $current_hr >= 12 && $current_hr < 17:
$new_prefix = __( 'Good Afternoon', 'my-text-domain' );
break;
// Handle evening.
case $current_hr >= 17 && $current_hr < 25:
$new_prefix = __( 'Good Evening', 'my-text-domain' );
break;
}
// Now update the title.
$wp_admin_bar->add_node( [
'id' => 'my-account',
'title' => $new_prefix . ', ' . $base_title,
] );
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment