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 ensures 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 session starts, and some information is saved inside the cookie for later use. So how to check if a 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 is part of the WordPress API, and it makes it very easy for you to get the logged in status of any registered user in order to check if a user is logged in.

What is the is_user_logged_in() function

The is_user_logged_in() function in WordPress is a conditional function that checks whether a user is currently logged into the website. It returns a boolean value (true or false) to indicate the user’s authentication status, helping developers control access to specific content or functionalities based on user login status. This function is a fundamental tool for building secure and personalized user experiences on WordPress websites.

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

<?php
if (is_user_logged_in()) {
    echo "You are logged in.";
else { 
    echo "Log in to access content.";
?>

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:

function my_check_if_user_is_loggedin()
{
    if (is_user_logged_in()) 
    {
        // do something
    }
}
add_action('init', 'check_if_user_is_loggedin_function');

 

Since every user has a user id in WordPress we can 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:

if (get_current_user_id()) { 
// display content here
 }

 

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 users login to their WordPress account, Users Insights will detect it, showing a green dot next to their username in the user table. This is very handy for WordPress admins because they will know how many people are 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 user authentication is done in WordPress. We have provided a few different ways that you can check if a user is logged in, with custom code and with a WordPress plugin, which you can use to check if a user is logged in, and test your own website or your client’s websites.

Gain Valuable Insights From Your WordPress User Data