To check if a user is logged in or not, we can use WordPress is_user_logged_in function. This function accepts no arguments and only checks the current user authentication status. the is_user_logged_in function returns a boolean value, true if the user is logged in, false if not. Be with me if you want to see this function in action.
How to check if the current user is logged in when developing WordPress?
WordPress introduced is_user_logged_in()
in version 2.0 to let developers determine if the current visitor is registered or is a guest.
is_user_logged_in
function syntax
The function has clear syntax and does not accept any parameter so that you can use it directly in your conditions:
is_user_logged_in()
The result of executing this function is a boolean value that replicates the visitor’s registration status.
The is_user_logged_in()
uses wp_get_current_user() under the hood. is_user_logged_in()
gets the user object from wp_get_current_user()
, if the object exists, it returns true; otherwise, false.
is_user_logged_in
function usage
In this example, we’ll use the is_user_logged_in()
to print an invitation to register on our website for visitors:
if(is_user_logged_in()) {
echo 'You can use our features, because you are registered!';
}
else {
echo 'welcome to our website, register to use our features!';
}
written by Mehdi Nazari about in WordPress WordPress Functions WordPress Plugin Development WordPress Theme Development
What do you think about "How to Check if the Current User is logged in WordPress"?