Recently my client asked me to write code for adding to ActiveCampaign subscriber list when submitting a Gravity form. There is addons available for most of the mail campaign. But not for ActiveCampaign. Here is how I achived that, though it seems bit complicated.
I have used the gravity form pre submission hook
add_action("gform_pre_submission_1", "cp_to_activecampaign");
Login to your activeCampaign account and get the form code. Extract the form fields including the hidden fields and prepare the varaibles.
You activecampaign form filed will look like these
<form id="_form_1133" accept-charset="utf-8" action="http://uraccount.activehosted.com/proc.php" enctype="multipart/form-data" method="post"><input type="hidden" name="f" value="1133" /> <input type="hidden" name="s" value="" /> <input type="hidden" name="c" value="0" /> <input type="hidden" name="m" value="0" /> <input type="hidden" name="act" value="sub" /> <input type="hidden" name="nlbox[]" value="10" /> <input type="text" name="fullname" /> <input type="email" name="email" /> <input type="text" name="field[2]" value="" /> <input type="submit" value="Subscribe" /></form>
Now prepare the varaiables.
$first_name = $_POST["input_18.3"]; //get first name from gravity form
$last_name = $_POST["input_18.6"]; //get last name from gravity form
$Email = $_POST["input_19"]; //get email id from gravity form
$url = 'http://uraccount.activehosted.com/proc.php'; //action of the active campaign form
$fields = array(
'field[1]' => urlencode($last_name),
'fullname' => urlencode($first_name),
'email' => urlencode($Email),
'f' =>'11118',
's' =>'',
'c' => '0',
'm' => '0',
'act' => 'sub',
'nlbox[]' => '6'
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
Submit to active campaign using curl
//open connection $ch = curl_init(); //set the url, number of POST vars, POST data curl_setopt($ch,CURLOPT_URL, $url); curl_setopt($ch,CURLOPT_POST, count($fields)); curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string); //execute post $result = curl_exec($ch); //close connection curl_close($ch);
All to gether add these to you functions.php
add_action("gform_pre_submission_1", "cp_to_activecampaign");
function cp_to_activecampaign($form){
$first_name = $_POST["input_18.3"];
$last_name = $_POST["input_18.6"];
$Email = $_POST["input_19"];
$url = 'http://uraccount.activehosted.com/proc.php';
$fields = array(
'field[1]' => urlencode($last_name),
'fullname' => urlencode($first_name),
'email' => urlencode($Email),
'f' =>'1118',
's'=>'',
'c' => '0',
'm' => '0',
'act' => 'sub',
'nlbox[]' => '6'
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
}
I will be happy to help you if you need any assistance. 🙂





