How to Get Logged In User Information in WordPress

Are you looking for the best way to get logged in user information in WordPress? In the previous, post I shared with you how to check if a user is logged in and also how to find the role of logged in user. In this post, I want to extend this by explaining how to get logged in user information in WordPress.  WordPress comes with a function that helps in getting user information.

Check if the User is Logged In

The first step to get the information of logged in the user needs to begin by checking if the user is logged in. I explained in details how to check if the user is logged in and the code is as follows:

function check_if_user_is_logged() {

// check if there is a logged-in user

    if( is_user_logged_in() ) {

      // If the user is logged in do stuff here

     }

}

After you check if the user is logged in the next step is to use the wp_get_current_user() function to get the current user object and begin extracting the information about the logged-in user.

To illustrate how you can implement this solution, I will create an action hook that hooks on the wp_head and displays the user information in a callback function. So the action hook is as follows :

add_action('wp_head',  'display_current_user_information');

Check the User Object: Get Logged In User Information

Before we can obtain and display the logged in user information, it helps to first print out the user data using a print_r so that we can evaluate the current user object and for purposes of understanding the general user object.

Add this code to functions.php and visit the frontend to see the user data or information is displayed in an array as shown in the image:

How to Get Logged In User Information in WordPress

You can now print all this information as you wish or use it in your logic. To access the user information you should make use of the object iterator -> and the respective key example user_email

The following is the example of code that displays first name, last name, email address and username:

add_action('wp_head',  'display_current_user_information'); 

function display_current_user_information() {
	
// check if there is a logged in user 
	
	if( is_user_logged_in() ) { 
	 
	 $current_user = wp_get_current_user(); 
	 
	   //user first name  
	   echo "First Name : " . $current_user->user_firstname ."</br>"; 
	    
		//user last name  
		echo "Last Name : " .$current_user->user_lastname ."</br>"; 
		
		//User email 
	    echo"Email Address : " . $current_user->user_email ."</br>"; 
		 
		 //username 
		echo "Username  : " . $current_user->user_login ."</br>";
	}

}

You can add this code to your functions.php or the plugin file to test and see the result as shown on the image below:

How to Get Logged In User Information in WordPress

Conclusion

In this post, we have looked at how you can combine the code to check if the user is logged in and also get all the data or information about the logged in user. The most important part of this post is to understand the role that wp_get_current_user() function plays since it is responsible for getting all that data and making it available.

Finally, It is also worth noting that in some outdated tutorials they use this method – get_currentuserinfo(); which has been deprecated and should not be used to get information of logged in user.

Comments are closed.