How To: Get the Current Logged in User ID in WordPress

When developing custom themes and plugins for WordPress there are times you will need to get the logged in user’s ID. There are a few different ways to accomplish this, but I’m going to show you the easiest method. To get the user’s ID you’ll use the get_current_user_id() function like so:

$current_user_id = get_current_user_id();
echo 'Your User ID is: ' .$current_user_id;

The get_current_user_id() function will return the currently logged in user’s ID, or 0 if a user is not logged in. Another popular method, which requires a bit more code, is the get_currentuserinfo() function. The method I described above actually uses the wp_get_current_user() function, which is a wrapper for get_currentuserinfo(). So using get_current_user_id() is just a faster method for retrieving the same data. This function was added in WordPress 3.0 as part of the Multisite code merge into WordPress.

To learn more about the get_current_user_id() function check out the Codex article or consult the WordPress core.

Enjoy this post? Be sure to subscribe to my RSS feed and my WordPress Tips and Tricks Newsletter! Also check out my WordPress books: Professional WordPress Third Edition & Professional WordPress Plugin Development