Retrieving Currently-Logged-in User Data
Many WordPress websites need to retrieve the currently logged in user information, whether it’s for Membership functionality, e-Commerce or for something like a Commenting section. Since the beginning, WordPress allowed developers to retrieve the user data.
This capability was always a part of the core, but the retrieval method behind has since changed. As a developer, you should update your code accordingly.
Retrieving Data The Old Way
get_currentuserinfo() function was used to place the current user’s information into the
$current_user global variable.
You could do the same using various separate global variables:
In both methods, the
get_currentuserinfo() function was used to alter global variables — which is a bad practice. This is why this function was deprecated in WordPress 4.5.
Retrieving Data The New Way
If you need the current logged in user information, you should retrieve the data into a new, regular variable. You can do so using wp_get_current_user(). Note that it’s not a new function, it’s actually been in core for years (since WordPress 2.0.3).
This function retrieves the the current user’s information and doesn’t affect any global variables. You may use it this way:
Updating your Code
If you are a plugin/theme developer, you need to update your code. It’s not a major update, here’s an example:
That’s all. Now go refresh your code!