This is one of those scenarios where I couldn't find the sample code for what I needed - and no one has yet to build a good plugin for this functionality. Not even Active Campaign!!
So, if you need to send a subscriber to your Active Campaign account when they sign up as a new user on your WordPress site, then this article has you covered. This is actually more for me so I can reference this in the future again when I need it!
The code below will go directly into your functions.php file inside of your WP theme. You'll have the option of adding users to a specific list as well.
You'll need to get your API URL and API Key from your Active Campaign account.
function add_user_to_active( $user_id ){
$user_info = get_userdata($user_id);
$user_roles = $user_info->roles;
// If this is a subscriber, then let's do this!
if ( in_array( 'subscriber', $user_roles, true ) ) {
$first_name = $user_info->first_name;
$last_name = $user_info->last_name;
$user_email = $user_info->user_email;
$url = 'https://YOURURL.api-us1.com';
$params = array(
'api_key' => 'YOUR_LONG_API_KEY',
'api_action' => 'contact_add',
'api_output' => 'serialize',
);
// here we define the data we are posting in order to perform an update
$post = array(
'email' => $user_email,
'first_name' => $first_name, // Optional if you don't collect this in your signup form
'last_name' => $last_name, // Optional if you don't collect this in your signup form
'p[3]' => 3, // example list ID (REPLACE '3' WITH ACTUAL LIST ID, IE: p[5] = 5)
'status[3]' => 1, // 1: active, 2: unsubscribed (REPLACE '3' WITH ACTUAL LIST ID, IE: status[5] = 1)
'instantresponders[3]' => 0, // set to 0 to if you don't want to sent instant autoresponders
);
// This section takes the input fields and converts them to the proper format
$query = "";
foreach( $params as $key => $value ) $query .= urlencode($key) . '=' . urlencode($value) . '&';
$query = rtrim($query, '& ');
// This section takes the input data and converts it to the proper format
$data = "";
foreach( $post as $key => $value ) $data .= urlencode($key) . '=' . urlencode($value) . '&';
$data = rtrim($data, '& ');
// clean up the url
$url = rtrim($url, '/ ');
// This sample code uses the CURL library for php to establish a connection,
// submit your request, and show (print out) the response.
if ( !function_exists('curl_init') ) die('CURL not supported. (introduced in PHP 4.0.2)');
// If JSON is used, check if json_decode is present (PHP 5.2.0+)
if ( $params['api_output'] == 'json' && !function_exists('json_decode') ) {
die('JSON not supported. (introduced in PHP 5.2.0)');
}
// define a final API request - GET
$api = $url . '/admin/api.php?' . $query;
$request = curl_init($api); // initiate curl object
curl_setopt($request, CURLOPT_HEADER, 0); // set to 0 to eliminate header info from response
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1); // Returns response data instead of TRUE(1)
curl_setopt($request, CURLOPT_POSTFIELDS, $data); // use HTTP POST to send form data
//curl_setopt($request, CURLOPT_SSL_VERIFYPEER, FALSE); // uncomment if you get no gateway response and are using HTTPS
curl_setopt($request, CURLOPT_FOLLOWLOCATION, true);
$response = (string)curl_exec($request); // execute curl post and store results in $response
// additional options may be required depending upon your server configuration
// you can find documentation on curl options at http://www.php.net/curl_setopt
curl_close($request); // close curl object
if ( !$response ) {
die('Nothing was returned. Do you have a connection to Email Marketing server?');
}
} // END if is a subscriber
}
// This function is triggered whenever a new user
// signs up for an account on your WordPress website
add_action('user_register', 'add_user_to_active', 10, 1);
So there you have it! Everytime a user is added to WordPress (as a subscriber) they will be added to a specific list in your Active Campaign account.
Side note - there is a plugin that claims to do this but in my testing it didn't work at all.