How to check if user is logged in WordPress

In this article we’ll learn how to check if a user is logged in to WordPress and how to make sure that only logged in users can access special features and content on your WordPress website.

Most WordPress websites have some area where users can login with their existing user accounts. This is usually in order to makes sure that only logged in users can access special features and content. WordPress user authentication is mainly done with cookies. A cookie is set when a WordPress user sessions starts, and some information is saved inside the cookie for later use. So how to check if user is logged in WordPress? You can retrieve this data through the WordPress API functions.

wordpress check if user is logged in

There are different options for checking the logged in status of a WordPress user. A common solution among WordPress developers is to use the is_user_logged_in() WordPress function. This is a built-in function that it is part of the WordPress API and it makes it very easy for you to get the logged in status of any user.

The is_user_logged_in() function returns True or False depending on the condition on the current user. If the current user is logged in it will return True, otherwise it will return false. So the simples usage is as a if else statement like in the following PHP code snippet:

But you can also create your own WordPress function that you can add in the functions.php file of your WordPress theme and control its execution via some action hook:

 

Since every user has a user id in WordPress we cal also alternatively use the get_current_user_id() function. The get_current_user_id() will return the current user id (an integer), or will return 0 if the user is not logged in. This function can be used in the header.php for example as it should return the correct value after the WordPress login, once the user session has started and the WordPress user has been assigned user id:

 

How to see what users are currently online WordPress

Do you want to keep track of your online users. If you are running a community site or forum where there are people posting content and reading posts made by others, then it can be very important for you to know the number of people currently viewing your WordPress website.

Users Insights activity tracking feature automatically detects and shows which users are currently online. Every time WordPress user login to their WordPress account, Users Insights will detect it and it will show a green dot next to their username in the user table. This is very handy for WordPress admins because they will get an idea of how many people are currently logged into their website.

In addition to your user’s online status, Users Insights also stores the last time a user was online. This information can be viewed in a column called “Last Seen.” If you aren’t seeing this column in your table, click on the heading of that column from the “eye” menu. By clicking on the column headings of your user list, you can order users by most recently active, as shown below:

wordpress user online last seen less than

Conclusion

In this tutorial we have looked into how the user authentication is done in WordPress. We have provided few different way that you can check if a user is logged in which you can use to test your own website or your client websites.