How to get user email in WordPress

In this article, we’ll explore various methods and functions that enable the retrieval of user emails in WordPress. These methods offer versatility in user management scenarios, providing administrators and developers with flexible tools to improve communication and streamline tasks across different aspects of a WordPress website.

Getting email from WordPress user object

The WP_User class is a core part of WordPress representing a user object. It encapsulates user data and provides methods to interact with and manipulate user information within WordPress. In WordPress, certain functions return a WP_User instance. Additionally, you can instantiate WP_User class with a user ID, so that you can access and manage the user attributes.

wordpress get user email

You can access the user’s email address by using the user_email property of the WP_User object:

$user_id = 1;
$user = new WP_User($user_id);
echo $user->user_email;

How to get the email of the currently logged-in WordPress user

To get the email from the current user, we can use the wp_get_current_user() function:

if(is_user_logged_in()){
  $user = wp_get_current_user();
  echo $user->user_email;
}

This code first checks if a user is currently logged in to the WordPress site using the is_user_logged_in() function. If a user is logged in, it retrieves the current user’s information using wp_get_current_user() and stores it in the $user variable. The result is an instance of the WP_User class, and therefore the email is retrieved by accessing the user_email property.

How to get user email by user ID

To get the WordPress user email by ID, we first need to get the user object by ID. This can be accomplished by using the get_user_by() function:

$user_id = 123;
$user = get_user_by('id', $user_id);
if($user){
  echo $user->user_email;
}

Using the get_user_by() function, we retrieve the user object based on the provided ID and store it in the $user variable. Following this, we check if the user exists by checking if the $user object returns a truthy value. If a user does not exist with the given ID, the get_user_by() function will return false. If a user is found using the provided ID, we can get the user email address by using the user_email property.

How to get user email by username

Similarly to the previous example, we can get the user email by a user’s username by using the get_user_by() function. To find the WordPress user object we pass the “login” option as a first parameter and the user’s username as a second parameter. After this if a user exists with the given username, we can access the user’s email by using the user_email property.

$username = 'admin';
$user = get_user_by('login', $username);
if($user){
  echo $user->user_email;
}

How to get the email of a WordPress post author

If you have a post object and want to retrieve the author’s email, you can use the post_author property of the post object to access the author’s ID and then retrieve their email using get_the_author_meta(). Here’s an example:

$author_id = $post->post_author;

if ($author_id) {
  $author_email = get_the_author_meta('user_email', $author_id);
  echo $author_email;
}

How to get all WordPress user emails

To retrieve the emails of all WordPress users, you can use the get_users() function to fetch user data and then access the email information for each user. Here’s an example:

$users = get_users(); // Retrieve all users
if ($users) {
  foreach ($users as $user) {
    $user_email = $user->user_email; // Get email for each user
    echo $user_email . '<br>'; // Output or store the email as needed
  }
}

This code snippet retrieves all WordPress users using get_users(), and then, if users are found, it iterates through each user to access their email address using $user->user_email. You can modify the output or storage method based on your specific requirements. For example, you can store them in an array or display them on a page.

Alternatively, you can use a plugin like Users Insights to display and optionally export all user emails. Users Insights includes an email column, along with many other available user fields. You can search and sort the users by their email. You can also apply different criteria to segment the user list – you can filter the users by their activity and data from 3rd party plugins.WordPress get user email

Conclusion

This article has covered various ways to get user emails in WordPress. We discussed getting emails from user objects, the current user, using user IDs or usernames, and retrieving all user emails. Understanding these methods gives administrators and developers different options to get user email information as needed in WordPress.