While reading the following WordPress Codex page on Global variable usage, there is a bold line that states


Accessing other globals besides the ones listed below is not recommended.

** My question is in concern to using the global variable $current_user. Is it not recommended to use something like this in my functions.php file?

if ($current_user->display_name){
  echo '<p>welcome ' . $current_user->display_name . '</p>';

...or are there other alternative methods I should start building habits on without depending on WordPress global variables not mentioned on that Codex page?

I know what I am asking for probably doesn't have a black and white answer, but I would like to get the opinion from some of you guys how have been coding in wordpress for a while.


1 Answer 1


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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.