Hey @RLyftogt
Understand the different webhooks available in Klaviyo
Yes the code is written in PHP and is suitable for inclusion in the functions.php file of a wordpress theme or plugin.
here is sample code on creating a Function to send data to Klaviyo
function send_user_role_to_klaviyo($user_id, $role) {
$user = get_userdata($user_id);
$email = $user->user_email;
$api_key = 'YOUR_KLAVIYO_API_KEY';
$klaviyo_endpoint = "https://a.klaviyo.com/api/v2/list/YOUR_LIST_ID/members";
$data = array(
"api_key" => $api_key,
"profiles" => array(
array(
"email" => $email,
"properties" => array(
"Role" => $role
)
)
)
);
$args = array(
'body' => json_encode($data),
'headers' => array(
'Content-Type' => 'application/json'
),
'timeout' => 15,
'redirection' => 5,
'blocking' => true,
'sslverify' => false
);
$response = wp_remote_post($klaviyo_endpoint, $args);
if (is_wp_error($response)) {
$error_message = $response->get_error_message();
error_log("Klaviyo API request failed: $error_message");
} else {
error_log("Klaviyo API request succeeded: " . wp_remote_retrieve_body($response));
}
}
add_action('set_user_role', 'send_user_role_to_klaviyo', 10, 2);
I hope this helps
Cheers
Arpit