Hey @RLyftogt
This would involve a combination of custom code and Klaviyo’s API, along with setting up a webhook in wordpress. Then you will have to create a function to send data to klaviyo. Probably you will need a developer to be able to do this.
I hope this helps
Cheers
Arpit
Thanks Arpit. Would this be in functions.php or additional custom code in the Klaviyo toolkit? I’m happy coding in functions.php, but would need some guidance with the hooks to use etc. Would there be any reference material on this?.
Thanks again,
Robin
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