You should first check if the user is logged in, doing is_user_logged_in()
, if so you get the username.
function give_profile_name(){
if ( is_user_logged_in() ) {
$user=wp_get_current_user();
$name=$user->display_name;
return $name;
} else {
return "";
}
}
You can also change your if to check if the shortcode is returning Untitled.
if ( isset( $shortcode_tags['profile_name'] ) && $shortcode_tags['profile_name'] != "Untitled")