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

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.

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.