Quick Tip: get_currentuserinfo() is Deprecated, Use This Function Instead

The get_currentuserinfo() function was in WordPress Core since the beginning — since version 0.71. WordPress 4.5 finally deprecated the function in favor of wp_get_current_user().

 

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

The old 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:

Replace get_currentuserinfo() with wp_get_current_user()
Replace get_currentuserinfo() with wp_get_current_user()

 

That’s all. Now go refresh your code!

Share on facebook
Share on twitter
Share on linkedin
Share on reddit
Share on whatsapp
Share on email
Rami Yushuvaev

Rami Yushuvaev

I'm an entrepreneur, a web developer and a blogger. I’ve contributed code to each and every release since WordPress 2.8. I'm the Hebrew GTE responsible for the translation and the release of WordPress Hebrew version. The founder GenerateWP.com and several other WordPress related projects. I work mainly with Israeli startups and hi-tech companies, providing development services.

One comment:

  • Thomas

    I cannot believe I missed this. Thanks for the info. I guess I am going to have to “refresh my code” in a few projects. Love the site by the way. Great work!

    Reply

Leave a Reply

Subscribe to our Blog

Receive email notifications when new blog posts are published.