Last active
October 11, 2023 10:20
-
-
Save finalwebsites/fb0216d4673ed766ec568a50f9fe0729 to your computer and use it in GitHub Desktop.
Elementor form action hook to send suscribers to the EmailOctopus API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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', | |
)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to build an Elementor form that connects to the EmailOctopus API
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.