'Wordpress to Sendy Automation with PHP

I'm hoping someone can help me with a PHP coding problem I'm facing. Disclaimer: I'm totally new to PHP but I'm doing my best to learn - so bear with me if I ask some 'stupid questions' :D.

I'm trying to make it so that when users sign-up to the website (currently using Ultimate Member plugin), their information is automatically loaded into Sendy - providing they select an option confirming it is okay to do so (in order to be GDPR compliant).

As things stand, when a user signs up to the site, a mandatory radio menu assigns the user a role, either 'Yes please' if they wish to receive marketing information, or 'No' if they don't. I configured Ultimate Member and Wordpress in this way in a hope that the php code at the following links (primarily link 2 as it is the most recent and best fitting) would work with a few edits: Link 1 Link 2

I used the above as inspiration to write my own version, however I have yet been able to get either working at all despite a large number of edits and repeated attempts.

I think the issue is that the original code was written mainly with WooCommerce in mind. I'm simply looking for any users who register and select 'Yes please' (thus assigning them this role) are automatically loaded into the Sendy instance. The Ultimate Member plugin may be preventing this somehow, I'm not quite sure.

Here is where I have gotten to thus far:

<?php
add_action( 'user_register', 'add_user_to_sendy_list' );

function add_user_to_sendy_list( $user_id ) {
  $api_key = '***SENDY API KEY***';
  $list = '***SENDY LIST***';
  $url  = '***SENDY INSTALL URL/subscribe***';
  
  //Get User info on user passed in from registration via user_id
  $user = get_userdata( $user_id );
  $email = $user->data->user_email;
  $name  = $user->data->user_first_name;
  $name  = $user->data->user_last_name;
  //Get role on user passed in from registration via user_id. Not needed for Sendy only for filter code below.
  $role = $user_info->roles;

  $args = array(
    'body' => array(
      'email' => $email,
      'name'  => $name,
      'boolean' => true,
      'list'    => $list
    )
  );

  //If new account doesn't have the 'Yes please' role relating to marketing preferences, don't do anything.
  if (!in_array('Yes please', $role)) {
    return;
  }
  
  $result = wp_remote_post( $url, $args );
}

Many thanks for your help in advance.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source