1

I just had a look at Global variables in WP and I discovered that a lot has changed since the last time I looked.

$current_user is no longer listed. Does that mean this and many other (old) globals will be "phased out" from future WP versions?

6

The $current_user is still a valid global, however like all globals, you should avoid relying on them.

If you need the current user in a function, do this instead:

$current_user = wp_get_current_user();

Also be aware that this may return false if no user is logged in on the current request, while the global $current_user may simply be an invalid instance of the WP_User (with an ID of zero). This is for backwards compatibility, and a wonderful reason to not rely on the globals.

1

When it snows in hell.

WordPress has extreme backwards compatibility commitment. It is very unlikely anything like global variable will be just removed, without much advance notice and being in deprecated status for literally years.

However that is no excuse for sloppy code practices and really anything that can be accessed via proper API functions (rather than globals), should be whenever at all possible.

PS I would not rely on Codex as indicator of state of code base too much. When wondering about code - look at code.

  • Thanks for the feedback. I should use proper API, but sometimes I get lazy :) – Steven Jul 18 '13 at 22:28
  • @Steven think of the kittens, it will help to keep it real – Rarst Jul 18 '13 at 22:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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