WP-Mix

A fresh mix of code snippets and tutorials

Get All User Email Addresses in WordPress

Putting together a mailing list? Here is an easy way to get all user email addresses from your WordPress database.

So there are many ways to go about getting all user emails, but this is the method that I find the easiest. What we’re going to do is create a custom page template that displays all user names and emails. So you can just copy/paste, grab, gulp, and go.

Step 1: Create and upload a custom page template

Create a new custom page template in your theme and add the following code:

<?php /* Template Name: Get User Emails */ 

$all_users = get_users();
echo '<ol>';
foreach ($all_users as $user) {
	echo '<li><span>' . esc_html($user->user_email) . ' : ' . esc_html($user->display_name) . '</span></li>';
}
echo '</ol>'; 
?>

This template code first uses get_users() to grab all user data. It then loops through the $all_users array and displays the email address and display name for each user. Much more data may be displayed for each user, take a look at the reference link for more info on get_users().

Step 2: Create a custom page

With your custom page template uploaded to the server, log in to your Admin Area, create a new page, and assign it to the custom page template, “Get User Emails”.

Note: It is IMPORTANT to set this page as private or password-protected so that no other people will be able to view it online. Think about it, you’re going to be displaying the names and emails of all of your users. Please take steps to keep this data private.

Once the custom page template has been applied to your page, visit it on the front-end to get all user names and emails. They should be displayed in a nice unordered list, just ready for you.

Limit number of displayed users

If your site has more than a few hundred users, it makes sense to limit the number of displayed users. To do so, modify the get_users() function with a couple of parameters:

get_users('number=500&offset=0');

With this, the page now will display only the first 500 users. Then you can modify the number and offset to display other users as desired.

References

Learn more

Digging Into WordPressWordPress Themes In DepthWizard’s SQL Recipes for WordPress