Generate WordPress Users from Mailchimp Audience Members
Since there is Simple Mailchimp Sync plugin on my site that allows to automatically subscribe WordPress users to Mailchimp audiences, from time to time I receive a question – is it possible to do the opposite? To connect Mailchimp list to a WordPress website somehow and create users from the emails?
Something like this:

The answer is yes for sure and in this tutorial let’s figure it out how to do that.
Get Members from a Mailchimp Audience (List)
Our first goal is to connect to Mailchimp using its API and get all the members data from a specific audience.
$api_key = '';
$audience_id = '';
$response = wp_remote_get(
// there is a limit to 100 audiences, should be enough
add_query_arg(
array(
'status' => 'subscribed',
'count' => 20, // default 10, maximum 1000
'offset' => 0,
),
'https://' . substr( $api_key, strpos( $api_key, '-' ) +1 ) . '.api.mailchimp.com/3.0/lists/' . $audience_id . '/members/'
),
array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( 'user:'. $api_key )
)
)
);
if( 200 === wp_remote_retrieve_response_code( $response ) ) {
$members = json_decode( wp_remote_retrieve_body( $response ), true );
$members = $members[ 'members' ];
print_r( $members );
}
/*
Array
(
[0] => Array
(
[email_address] => test@rudrastyh.com
[full_name] => Misha Rudrastyh
[email_type] => html
[status] => subscribed
[merge_fields] => Array
(
[FNAME] => Misha
[LNAME] => Rudrastyh
...
)
[interests] => Array
...
*/
Everything is pretty straightforward here, but just in a case a couple comments:
- If you don’t know where to get Mailchimp API key, please read this guide.
- If you don’t know how to get a list ID, here is how.
- For Mailchimp API requests I am using WordPress HTTP API, because why we should use anything else while we are working in WP environment? A little but more information about connecting to Mailchimp API using HTTP API you can find here.
What if there are thousands of members in your Mailchimp audience
In the example above I get only 20 members from a specific Mailchimp audience, but in the real life Mailchimp audiences have 1000 and more members.
How to deal with that if we have a limit of 1000 in /lists/{list_id}/members
endpoint. And even if we didn’t have that limit, creating a huge amount of users may lead to a server response time error.
The answer is – pagination + WP Cron. Roughly it will be like this:
wp_schedule_event( time(), 'hourly', 'rudr_create_100_users_hook' );
add_action( 'rudr_create_100_users_hook', function() {
$offset = get_option( '_members_pagination', 0 );
$members = rudr_get_members( $offset );
if( $members ) {
update_option( '_members_pagination', ( $offset + 20 ) );
rudr_create_users( $members );
} else {
wp_clear_scheduled_hook( 'rudr_create_100_users_hook' );
delete_option( '_members_pagination' );
}
} );
Step by step explanation:
- Line
1
. As you can see I used a hourly schedule, which is of course doesn’t make any sense, but that’s the minimum default timeframe. I recommend you to create your own one usingcron_schedules
filter hook. - Line
5
. I decided to store a current pagination parameter in WordPress options. You can also use transient cache (I think it could create issues) or user cookies. The default offset value is zero as you can see. - Line
7
. Functionrudr_get_members()
is our custom function which actually contains the code from the first step. - Line
11
.rudr_create_users()
is another custom function from the next step.
Create WordPress Users from List Members
// we need this line for the wp_new_user_notification() function
require_once ABSPATH . WPINC . '/pluggable.php';
foreach( $members as $member ) {
if( email_exists( $member[ 'email_address' ] ) ) {
continue;
}
$user_id = wp_insert_user(
array(
'user_login' => $member[ 'email_address' ],
'user_email' => $member[ 'email_address' ],
'user_pass' => wp_generate_password(),
'role' => 'subscriber',
'first_name' => $member[ 'merge_fields' ][ 'FNAME' ],
'last_name' => $member[ 'merge_fields' ][ 'LNAME' ],
)
);
if( is_wp_error( $user_id ) ) {
continue;
}
// it is WordPress-styled email but you can create your custom one
wp_new_user_notification( $user_id, null, 'user' );
}
What do you think guys? Do you need a plugin for that? Or I can add this functionality into my Simple Mailchimp Sync plugin. Let me know in comments.


Misha Rudrastyh
Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.
Need some developer help? Contact me