The WordPress admin bar often greets the user with a friendly “Howdy, [Username]” in the top right corner. If you’d like to change this greeting to something else, this snippet is exactly what you need.
This code snippet replaces the default “Howdy,” greeting with a custom message. In the given snippet, it’s replaced with “Welcome,” but you can change it to anything you prefer. The function wpcode_snippet_replace_howdy
is called when the ‘admin_bar_menu’ hook is triggered. This hook is used to customize the WordPress admin bar.
The function works by getting the ‘my-account’ node from the admin bar (which is the area that contains the “Howdy, [Username]” text), and then replacing the “Howdy,” part of the node’s title with the custom greeting. It then adds the updated node back to the admin bar.
To change the greeting, simply modify the $new_howdy
variable to contain your preferred greeting. Be sure to include the trailing comma if you want it to appear in the greeting.
function wpcoder_snippet_replace_howdy( $wp_admin_bar ) {
// Edit the line below to set what you want the admin bar to display intead of "Howdy,".
$new_howdy = 'Welcome,';
$my_account = $wp_admin_bar->get_node( 'my-account' );
$wp_admin_bar->add_node(
array(
'id' => 'my-account',
'title' => str_replace( 'Howdy,', $new_howdy, $my_account->title ),
)
);
}
add_filter( 'admin_bar_menu', 'wpcoder_snippet_replace_howdy', 25 );