You can use wp_get_current_user()
instead of using the $current_user
global.
$my_current_user = wp_get_current_user();
if ( $my_current_user->display_name ) {
echo '<p>Welcome ' . $my_current_user->display_name . '</p>';
}
Note that I used $my_current_user
instead of $current_user
as a variable name, because you can run into trouble if you use the global variable’s name.