Skip to content

Instantly share code, notes, and snippets.

@finalwebsites
Last active October 11, 2023 10:20
Show Gist options
  • Save finalwebsites/fb0216d4673ed766ec568a50f9fe0729 to your computer and use it in GitHub Desktop.
Save finalwebsites/fb0216d4673ed766ec568a50f9fe0729 to your computer and use it in GitHub Desktop.
Elementor form action hook to send suscribers to the EmailOctopus API
<?php
// place this code into your functions.php file
// Check the notes below for further instructions
// add here the EmailOctopus API key
define('EO_API', '00000000-0000-0000-0000-000000000000');
// get the fields from a specific list from EmailOctopus
function get_emailoctopus_list_fields($list_id) {
$url = 'https://emailoctopus.com/api/1.6/lists/'.$list_id.'?api_key='.EO_API;
$response = wp_remote_get( esc_url_raw( $url ) );
$api_response = json_decode( wp_remote_retrieve_body( $response ), true );
$fields = array();
// store all the field tags in one array
foreach($api_response['fields'] as $field) {
$fields[] = $field['tag'];
}
return $fields;
}
// Check if a subsciber alreay exists on EmailOctopus
function check_emailoctopus_subscriber_exists($email_adr, $list_id) {
$id = md5(strtolower($email_adr));
$url = 'https://emailoctopus.com/api/1.6/lists/'.$list_id.'/contacts/'.$id.'?api_key='.EO_API;
$response = wp_remote_get( esc_url_raw( $url ) );
$api_response = json_decode( wp_remote_retrieve_body( $response ), true );
if ($api_response['error']['code'] == 'MEMBER_NOT_FOUND') {
return 'new';
} else {
// Sucscriber exists > return member ID
return $api_response['id'];
}
}
// Action hook to send form data to the EmailOctopus API
add_action( 'elementor_pro/forms/new_record', 'subscribe_emailoctopus', 10, 2 );
function subscribe_emailoctopus($record, $handler) {
$raw_fields = $record->get( 'fields' );
$fields = [];
foreach ( $raw_fields as $id => $field ) {
$fields[ $id ] = $field['value'];
}
$test = check_emailoctopus_subscriber_exists($fields['EmailAddress'], $fields['list_id']);
if (!empty($fields['EmailAddress']) && !empty($fields['list_id'])) {
$valid_fields = array();
$tags = array();
$eo_fields = get_emailoctopus_list_fields($fields['list_id']);
// check which form fields exists on your EmailOctopus list
foreach ($fields as $key => $val) {
if (in_array($key, $eo_fields)) {
$valid_fields[$key] = $val;
} elseif ($key == 'tag') {
$tags = array_map('trim', explode(',', $val));
if ($test != 'new') {
$update_tags = array();
foreach ($tags as $tag) {
$update_tags[$tag] = true;
}
$tags = $update_tags;
}
}
}
}
$data_array = array(
'api_key' => EO_API,
'email_address' => $fields['EmailAddress'],
'fields' => $valid_fields,
'tags' => $tags,
'status' => 'SUBSCRIBED'
);
$method = 'POST';
$url = 'https://emailoctopus.com/api/1.6/lists/'.$fields['list_id'].'/contacts';
if ($test != 'new') {
$url .= '/'.$test;
$method = 'PUT';
}
$data = wp_remote_post($url, array(
'headers' => array('Content-Type' => 'application/json; charset=utf-8'),
'body' => json_encode($data_array),
'method' => $method,
'data_format' => 'body',
));
}
@finalwebsites
Copy link
Author

finalwebsites commented Oct 8, 2023

How to build an Elementor form that connects to the EmailOctopus API

  1. Create an API Key in EmailOctopus (click in the upper right menu > Integrations & API)
  2. Place the code snippet in the functions.php file from your WP Child theme
  3. Replace the placeholder with your own API Key
  4. Create or open a page in Elementor Pro.
  5. Place the form widget in the desired location.
  6. For a simple form, you need 3 fields: 1 for the email address, 1 for the name, and 1 hidden field for the list ID in EmailOctopus.
  7. Change the ID (Advanced tab) for email and name to match the tag in EmailOctopus. For email, it's "EmailAddress," and for the name, we use the "FirstName" field. If you want to send more fields to EmailOctopus from your form, you'll need to create those fields there and name the tag the same as in your Elementor form.
  8. For the hidden field, set the ID to "list_id" and the value (Default Value) to the list ID from EmailOctopus. You can find this in Lists > Your list name > Settings tab.
  9. You can add tags with a hidden field too. Use for the ID, the string "tag" and add comma separated strings as values.
  10. You don't need to specify anything for "Actions after submit." Save the page.

Not yet an EmailOctopus user? Create your free account for max 2500 subscribers.

Disclaimer

The function checks for existing subscribers, but doesn't report any errors or notices on failure. Check your Elementor form and EmailOctopus settings if something isn't working. We tried the function for several websites and it works as it is. There is no guaranty. Be free to ask your questions via the comments form.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment