How to Check if a User is Logged-in in WordPress by Coding?

A brilliant Content Management System (CMS) was created for free for millions of users to create free websites without knowing how to write a single line of code.

You should know that 40% of websites launched worldwide are built using WordPress. The simple admin dashboard that is mostly self-explanatory and the wide range of functionalities WordPress supports make it a unique piece of application software.

To log in to the admin dashboard users log in and access the functionalities. Wait a minute! Who are these users you are suddenly talking about?

Who are Users of WordPress?

People who use a website built on WordPress are the users of that WordPress website. They have different roles from- the users who publicly view the website to the administrators who have complete control of the website.

What are User Roles in WordPress?

There are several roles that WordPress offers. Each of these roles will have credentials, a login, and a password, to log in and access the functionalities.

Using these credentials, once a user logs in, the functionalities will be put forth based on their privileges.

The following are the roles available in WordPress.

Super Admin

In WordPress, there is a possibility that multisite can be built. A user who gets to access the site network administration features.


An admin with a slug as an administrator gets to access the administration features within a single site.


An editor with a slug as an editor can publish, and edit posts of his and other users of whom he is given privilege.


An author with a slug as an author can publish and edit their posts, not other users’ posts.


A contributor with a slug as a contributor is a little lower in privileges that they can only edit and not publish posts.


A subscriber with a slug as a subscriber can only manage their profiles and nothing else.

How do I find Users on WordPress?

To find the users on your WordPress you need to login into the Admin Dashboard with your username and password, and access the user menu on the left sidebar.

Once you log in, use the left sidebar to access the menu by the name Users.

Access the Users Link in the sidebar

Once you access that link, a list of users will be displayed on your right side. These are the users registered on your website.

Find the user list from the user menu on the left sidebar

Can WordPress have Multiple Administrators?

Yes! WordPress can have multiple administrators though you can add an administrator when you are logged in as an administrator.

To add one more administrator, you need to follow the below steps, just like how you add any other user.

Go to the list of users, from the previous section.

Click on the add button:

Add users by clicking on the Add New button

Fill out the below form and select Administrator as the role in the drop-down corresponding to it.

Select Administrator from the drop-down and Add New User

Once you add the new user, you have successfully added the new user. You can check for yourself by listing the users.

You can even log out and log in back with the new credentials and confirm that you have added the new administrator.

What we have learned till now is using the admin dashboard. What if we want to show a particular space only to the users who have logged in, to be specific, to the people who have roles?

Let us explore that in the following section.

How do I Know if a User is Logged Into WordPress or Not?

This can be achieved pretty easily. Thanks to the built-in function that WordPress offers. is_user_logged_in().

To do this, we suggest that you be familiar with PHP, a programming language that is used to build WordPress. You might understand it even otherwise.

As a prerequisite and to be safe with your current website, it is always advisable to have a child theme installed. A child theme is a sub-theme of a theme that is installed and activated with a few files extending some features where required. Check out this article if you want to know if WordPress themes affect SEO.

How to Check if a User is Logged in to WordPress?

You need to write the code in any of the PHP files that are there in the theme folder or the theme file editor.

To go to the theme file editor all you need to do is to go to themes in the left sidebar and click on the “Theme File Editor” sub-menu.

Theme File Editor from the sub-menu

Once you visit that page, you need to agree to the popup and continue.

Click on I understand and continue

You can now edit the PHP files from there or if you are using a child theme, go to the parent theme, copy the PHP file you want to edit, and start writing the code from there.

Is not the User Logged in to WordPress?

You can start writing the following code in the relevant place and save the file right there at the bottom of the page.

The code to check if the user is logged in can be used in the code as follows:

if (is_user_logged_in()) {
   echo 'The user has logged in';
} else {
   echo 'The user has logged out';

This code can be used in any of the PHP files like function.php, page.php, index.php, sidebar.php, etc.

How do I Find User Roles in WordPress?

You need to have access to the list of users, only then will you be able to find the user roles. Once you get into the list of users from the left sidebar, you can find a separate column to display the role associated with a particular user. Like so:

Under the column name, each user will be shown their role.

Find User Roles in WordPress Programmatically

Certain times, when you need to add functionality based on the roles of the users then you need to know what it takes to do that.

Since one user can have more than one role, you need to have an array object for storing the roles. Let us see in the below code, how we can achieve this.

function get_user_roles() {

  if(is_user_logged_in()) {
    $user = wp_get_current_user();
    $roles = (array) $user->roles;
    foreach (roles as $role)
        echo $role;


The above program starts with checking if the user has logged in. If the user has logged in, we need to get the current user by using the built-in function – wp_get_current_user();

As told earlier, since there will be multiple roles associated with a user, we will need an array to store the multiple roles.

In the function, we will be printing the array by looping it in a for loop. What will be printed will be the name of the roles associated with the user.

How do I Check if a WordPress User is Logged in Javascript?

There is a simpler way in Javascript than PHP, or you can say both are equally easier.

In WordPress, there will be a body tag that comes along with HTML. In addition to that, every good WordPress theme should have a body_class() in the <body> tag.

Using this body_class() if we write this code, we will get to if the user is logged in or not.


Similar Posts


Leave a Reply

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

16 − two =