How to Check if User is Logged In WordPress (PHP Function)

Developing for WordPress and want to know how check if a user is logged in? Keep reading for useful user PHP code snippets.

Sometimes you want to add functionality or display something only for logged in users. This is easy using WordPress’ built-in is_user_logged_in() function. This quick tip will show you how to check if a user is logged in WordPress.

To use this you’ll have to be familiar with programming in PHP. If you are editing a theme we recommend creating a child theme first. This will allow you to edit code in your theme without it breaking when the parent theme is updated. Additionally you can add code with a custom plugin or Code Snippets plugin.


Check if User is Logged Into WordPress Function

Here’s an example using the is_user_logged_in() function to display a logout link for logged in users and a login link for logged out users.

<?php
if ( is_user_logged_in() ) {
echo 'Welcome, logged in user. <a href="'.wp_logout_url().'">Click here to logout</a>.';
}else{
echo 'Please login by <a href="'.wp_login_url().'">clicking here</a>.'
}

You can use this in your theme’s function.php to add functionality specific to logged-in users. It will also work in your theme’s index.php, archive.php, single.php, etc for all kinds of functionality for logged in users.


Check if Current User is Administrator in WordPress

If you want to add functionality only for logged in admins this can be done with the current_user_can() function. By using current_user_can(‘administrator’) in an if statement it’ll allow you to check if the current user is a site admin.

<?php
if( current_user_can('administrator') ) {
echo 'This will display for WordPress admins only.';
};

Additionally, you can target a specific capability of a user.

This is useful if you have created custom roles on your site. There are plenty of WordPress capabilities to target your if statement. For example, manage_options is good for targeting admins while edit_posts is good for targeting editors.

<?php
if( current_user_can('manage_options') ) {
echo 'This user can manage WordPress options. (Settings Page)';
};

Additionally if you don’t want to use PHP you can change styling of the site using CSS when a user is logged in. WordPress adds the class “logged-in” to the body tag of your site when a user is logged in.

For example this snippet below will change the background color of your site when a user is logged in.

/* Change the background color for logged in users */
body.logged-in {
background-color: #BEBEBE
}

In addition to using is_user_logged_in() in your theme and functions.php you can also include this in a custom WordPress plugin for the same effect.

Picture of Andy Feliciotti

Andy Feliciotti

Andy has been a full time WordPress developer for over 10 years. Through his years of experience has built 100s of sites and learned plenty of tricks along the way. Found this article helpful? Buy Me A Coffee

9 Responses

  1. if( current_user_can('administrator') )

    is incorrect.

    current_user_can checks for a capability, not a role. For example:

    if( current_user_can('edit_posts') ) { }

    if( current_user_can('manage_options') ) { }

    Etc

    1. Thanks for the tip, I do agree that targeting a capability may be better but if you export the user’s capabilities using “get_userdata( get_current_user_id() )->allcaps” “administrator” is one of the ones assigned to an admin so the snippet works.

    1. You can use the code above in a PHP function to hide anything you want but if you aren’t too familiar with PHP using CSS might be easier. WordPress by default adds a class of “logged-in” to the body of the page so you can target an attribute for hiding using.
      body.logged-in .yourclassname {display:none!important};

  2. what if the user has logged in and is immediately directed to the landing page? If you haven’t logged in yet, you will be directed to the login page

    1. You could add a conditional statement and use the wp_redirect function to redirect. If your function is before html output it’ll instantly redirect. If it’s after html output (for example in body content of theme html) you can also try a javascript redirect but it’ll have a delay before moving the user. Hope this helps!

Leave a Reply

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

WordPress Tips Monthly
Get the latest from SmartWP to your inbox.