How to update get_currentuserinfo() for WordPress 4.5+

In one of my plugin, f(x) Profile Dashboard Widget, I use get_currentuserinfo() to get current user data, apparently it’s deprecated in WP 4.5.

Here’s the code I use to replace it with wp_get_current_user(): while maintaining backward compatibility.

Previous Code:

global $current_user;
get_currentuserinfo();

New Code (WP 4.5):

if( function_exists( 'wp_get_current_user' ) ){
    $current_user = wp_get_current_user();
}
else{
    global $current_user;
    get_currentuserinfo();
}

Leave a Reply

Your email address will not be published. Required fields are marked *