I recently was working on a project where the client had a Constant Contact account and Gravity Forms installed on their WordPress site. Using Gravity Forms’ add-on for Constant Contact it was easy to subscribe users to Constant Contact lists for those forms created with Gravity Forms. However, the project was also utilizing The Events Calendar with Event Tickets to accept event registrations. It would be nice to allow users to sign up for mailing lists when registering for an event.
I know that it is possible to use Constant Contact’s APIs to subscribe people to lists programatically. However, I figured it made more sense to leverage the already existing code in Gravity Forms, if possible. It turns out, after a little digging, it wasn’t difficult to use the existing code at all.
First, of course, you need to make sure that Gravity Forms is set up and working correctly with your Constant Contact account. This is done through the Gravity Forms’ Settings interface.
Then, looking at the code in the Constant Contacts add-on, I saw that there was a method used to subscribe users to a list,
subscribe_to_list. This is located in the
GF_ConstantContact class as part of the
Since we are using the
GF_ConstantContact class, the first thing we need to do is instantiate it. We do this and then use a method,
initialize_api, to get the ball rolling:
$gf_constant_contact = new GF_ConstantContact(); $gf_constant_contact->initialize_api();
initialize_api method takes care of authenticating with Constant Contact’s APIs.
We’ve gotten everything prepared, so let’s return to the method we first looked at,
subscribe_to_list accepts a single argument – an array of information pertaining to the subscriber. Here are the items of the array that I am providing to subscribe users to my Constant Contact mailing list:
list_memberships– An array of lists to subscribe the user to. Lists are identified by a unique key which we will discuss a little later.
email_address– An array with one item,
address, which is the user’s email address.
first_name– A string with the user’s first name.
last_name– A string with the user’s last name.
Here is an example of the array that I am using with the
$subscriber_details = array( 'list_memberships' => $lists_to_subscribe_user_to, 'email_address' => [ 'address' => 'firstname.lastname@example.org' ], 'first_name' => 'Bob', 'last_name' => 'Test' );
The only item that is a little tricky is the
$lists_to_subscribe_user_to. Like I said, it is an array… but how do we get the values needed to identify the mailing lists? Well, the way that I did it, was to view a Gravity Form that is integrated with my Constant Contact account. You may need to add the feed you want to use with your form first. Then you can view the Constant Contact Feeds for a form under Settings > Constant Contact:
Click on ‘Edit’ underneath your feed. The resulting screen will give you a dropdown for “Constant Contact List” with all of the email lists for the connected account. The ID of the list is not immediately visible, but if you right-click on the dropdown and inspect it in your browser’s developer tools, you will be able to see the value of the
<option> of each email list:
In my example, I want to subscribe users to the General Interest list, so I copy the value of that dropdown item to my clipboard. From there, I can add my email list ids to
$lists_to_subscribe_user_to. Remember, this is an array that we could use to subscribe the user to multiple lists. Even though I am passing a single identifier to the function, I am still going to use an array. Here is an example:
$subscriber_details = array( 'list_memberships' => array( '7ff4258a-3899-11ea-zzzz-zzzzzzzzzzzz' ) ),
Finally, now that you have all of your subscriber details packaged up, you can simply send them to the
$result = $gf_constant_contact->subscribe_to_list( $subscriber_details );
Gravity Forms will return either
true, for a successful subscription or a
WP_Error with information on why the attempted subscription failed.
I found that repurposing the Gravity Forms Constant Contact allowed me to not have to try to reinvent the wheel. I was then able to add a little bit of code to the
woocommerce_checkout_order_processed action to subscribe users upon registration to an event.