Will $current_user no longer be global?

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?

2 Answers
2

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.

Leave a Comment