The get_userdata function in WordPress is used to retrieve user data associated with a specific user ID. It returns an object with all the available user data, such as username, email, display name, and more.
This function is useful when you want to access and display specific user information on your WordPress site. It allows you to fetch user details programmatically, which can be used to create custom user profiles, display user information in templates, or perform any other operations that require user data.
Here’s an example usage code for the get_userdata function:
$user_id = 123; // Replace with the desired user ID
$user_data = get_userdata($user_id);
if ($user_data) {
// Accessing user data
$username = $user_data->user_login;
$email = $user_data->user_email;
$display_name = $user_data->display_name;
// Outputting user information
echo "Username: " . $username . "<br>";
echo "Email: " . $email . "<br>";
echo "Display Name: " . $display_name . "<br>";
} else {
echo "User not found.";
}
In the example above, we first specify the desired user ID (e.g., 123) for which we want to retrieve the user data. Then, we call the get_userdata function, passing the user ID as the parameter. If the user is found, we can access different properties of the returned object, such as user_login, user_email, and display_name, to display the user information on the website. If the user is not found, a message indicating that the user was not found is displayed.
Remember to replace the user ID (123) with the actual user ID you want to retrieve data for.
That’s how the get_userdata function can be used to fetch user data in WordPress.
WordPress snippets using the get_userdata function
-
Login Widget
-
Login Widget
-
"WooCommerce Subscription Order-Based Profile Post Creator and Redirect"
-
Block specific users to create an account
-
How to Send Email Notifications in WordPress when User Roles Change
-
How to Send Email Notifications in WordPress When a User Profile Updates
-
How to Customize User Registration Notification Emails in WordPress