I created a widget with the below-mentioned code however when I insert it, it says internal server error. I thought it might be Wordfence but when I deactivated it still showed the error.
Does the same thing happen if you enable one of the default WordPress themes? If so, then it’s not a theme specific problem. Maybe the authors of the code in question could help you out further?