How to Easily Check If a User Is Logged In to WordPress

You are currently viewing How to Easily Check If a User Is Logged In to WordPress

If you are a full-stack WordPress developer or just a website owner using code snippets, checking whether a user is logged in is an essential function you will need to use sooner or later.

Checking the condition of the user and triggering relevant actions is a common practice and is used by many functions and plugins.

This article will show you how to check if a user is logged in to WordPress and how to use it in the functions.php and template files.

Why Check if a User is Logged In to WordPress

As mentioned above, validating if a user is logged in is used in many scenarios. Moreover, it is one of the most used WordPress functions, and for a good reason.

There are thousands of reasons why you’ll need to check the user’s status, and here are only some of them:

  • Conditionally display login/logout links in your website’s header.
  • Use the logged-in users’ names to greet and welcome them.
  • Show WooCommerce prices for logged-in users only.
  • Display certain pages, like my account page, for logged-in users.

You probably got the point and understand by now why it is beneficial to check if a WordPress user is logged in.

Whether you want to use it for one of the reasons above or for a different one, we will show you how to do it.

How to Check if a User is Logged In

WordPress has a built-in function that we can use to determine if a user is logged in or not.

is_user_logged_in()

You can also use PHP logical operator to check if a user is NOT logged in (logged out) by adding an exclamation point (!) to the left of the function, like so:

!is_user_logged_in()

It is a simple function because it doesn’t take any variables and returns true if a user is logged in and false if not logged in.

We can then execute different kinds of actions based on the returned value.

For example, let’s greet all logged-in users with a Hello fan message and just Hello to all logged out users. To do that, we can use the is_user_logged_in() function in our functions.php or template files.

Use the is_user_logged_in() Function in the functions.php File

WordPress uses hooks (like anchors) to simplify adding content to certain page parts.

Therefore, we will need to add the message to the desired hook based on where we want it to appear.

If we want to display it above the header, we can use the init hook.

Using an FTP or from your WordPress dashboard, navigate to Appearance -> Theme File Editor and open the functions.php file of your child’s theme.

Note: please back up your website before editing core files.

Then, scroll to the bottom of the file and paste this function:

add_action('init', 'greet_logged_in_users');
function greet_logged_in_users() {
	if ( is_user_logged_in() ) {
		echo '<p>Hello fan!</p>';
	} else {
		echo '<p>Hello</p>';
  }
}
Check if WordPress user logged in PHP function

Once saved, visit your website and ensure you can see the message for the logged-in users. Then, log out and verify that the alternative message is displayed.

If you prefer to check the user status within a template file, continue to the next section.

Use the is_user_logged_in() Function in a PHP Template File

The second way to use the is_user_logged_in() function to check if the user is logged-in is by executing it in a template file instead of the functions.php file.

The advantage of using a template file is the option to position the function anywhere on the page, regardless if there is a hook to which we can attach it.

Let’s take the example above and display a greeting message to logged in users using one of our theme’s template files.

Because we want to display the message above the header, we will need to modify the header.php file.

Therefore, navigate to Appearance -> Theme File Editor and click on the header.php file from the right sidebar.

Then, place the is_user_logged_in() function in your desired position, and save the file.

<?php
  if ( is_user_logged_in() ) {
    echo '<p>Hello fan!</p>';
  } else {
    echo '<p>Hello</p>';
  }
?>
Check if WordPress user logged in template file

Once saved, visit your website and ensure that the correct message is displayed based on the user status.

Conclusion

The is_user_logged_in() is a default WordPress function to check if a user is logged in.

This article taught you to check if a user is logged in using the is_user_logged_in() function in different scenarios and file types.

Leave us a comment and tell us your preferred method to achieve this task.

Also, make sure to subscribe to our YouTube channel and like our page on Facebook.

PluginsForWP

PluginsForWP is a website specializing in redistributing WordPress plugins and themes with a variety of knowledge about WordPress, internet marketing, and blogging.

Leave a Reply