Skip to main content
Contributor I
August 14, 2024
Solved

Can I use the Wordpress event add a role to a user as a metric?

  • August 14, 2024
  • 3 replies
  • 129 views

Is it possible to update a users profile in Klaviyo when a user has a new role added in Wordpress? I have a number of custom User Roles which are added to Users in Wordpress at specific points. I would like the Users profile in Klaviyo to be updated with a custom property and then trigger a flow.

I’ve explored Zapier and Integrately but can’t see how to get this working.

Thanks in advance.

    This topic has been closed for replies.
    Best answer by ArpitBanjara

    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

    3 replies

    ArpitBanjara
    Principal User II
    Principal User II
    August 15, 2024

    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

     

    Arpit Banjara - Lead Email Marketing Specialist at Flowium
    RLyftogtAuthor
    Contributor I
    August 15, 2024

    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

    ArpitBanjara
    Principal User II
    Principal User II
    August 16, 2024

    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

    Arpit Banjara - Lead Email Marketing Specialist at Flowium