WordPress - Check if user is logged in

In WordPress, you can use the function is_user_logged_in() to check if a user is currently logged in. This function returns a boolean value of true if a user is logged in, and false if they are not.

Example:

<?php

if (is_user_logged_in()) {
  // User is logged in
} else {
  // User is not logged in
}

Watch a course Learn object oriented PHP

Note: You need to include wp-includes/pluggable.php file if you are using this function outside of WordPress files.