Question

Update:

I got this working with a different hook user_register. It looks like wpmu_activate_user was never triggering.

I would still like to have this happen after the user verifies and sets a password though. Can anyone help?

Current Code:

function xxx_whmcs_signup($user_id) {
    
   //Get User Fields
    $user_info2 = get_userdata($user_id);
    $id = $user_info2->ID;

   //Get Custom Fields
    $first_name = get_field('first_name_c', 'user_'. $id );
    $last_name = get_field('last_name_c', 'user_'. $id );
    $address1 = get_field('address_1', 'user_'. $id );
    $city = get_field('city', 'user_'. $id );
    $state = get_field('state', 'user_'. $id );
    $postcode = get_field('postcode', 'user_'. $id );
    $country = get_field('country', 'user_'. $id );
    $phonenum = get_field('phone_number', 'user_'. $id );
    $email = $user_info2->user_email;
    $password = $user_info2->user_pass;
    

   // Set WHMCS API Details
    $whmcsUrl = "xxx";
    $apiusername = "xxx";
    $apipassword = "xxx";
    
   //Return Fields
    $postfields = array(
        'username'  => $apiusername, //WHMCS Username
        'password'  => $apipassword, //WHMCS Password
        'action'    => 'AddClient', //WHMCS Action
        'firstname' => $first_name,
        'lastname'  => $last_name,
        'password2' => $password,
        'email'     => $email,
        'address1'  => $address1,
        'city'      => $city,
        'state'     => $state,
        'postcode'  => $postcode,
        'country'   => $country,
        'phonenumber' => $phonenum,
        'responsetype' => 'json'
        );
        
    // Call the API
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $whmcsUrl . 'includes/api.php');
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postfields));
    $response = curl_exec($ch);
    if (curl_error($ch)) {
        die('Unable to connect: ' . curl_errno($ch) . ' - ' . curl_error($ch));
    }
    curl_close($ch);
    
    // Decode response
    $jsonData = json_decode($response, true);
 
    
    // Dump array structure for inspection
    var_dump($jsonData);

    $file = fopen("wp-content/plugins/integrations/curloutput.txt", 'w+');
    fwrite($file, $response);
    fclose($file);    
    }

add_action('user_register',"xxx_whmcs_signup",10,1);

Original post:

I am currently trying to get fields that are populated during the Wordpress Signup process and post them to another API.

The fields that I need to get hold of are:

  • First Name
  • Last Name
  • Email address
  • Password (Hash)
  • and a few other Advanced Custom Fields

When I created the original code, I managed to get it to work perfectly if the user was already logged in and went to a specific page, but I can't seem to get the code to work during the sign up process (probably would have to happen after the activation as that is when the password is set).

The entire code is currently:

pruned

Because the fields are in Advanced Custom Fields, I have also tried:

pruned

This worked perfectly when the script was running from a shortcode on a page, but of course there are extra hurdles thrown in due to the user not being logged in during the sign up process. I assume this depends on what information I can pass through to my function from a hook.

I don't fully understand passing variables through to a function or how I would return the output of $meta so I can see what I'm working with, so it is potentially quite an easy task (fingers crossed). It is also possible that I am not using the correct/best hook?

Thanks in advance!

Was it helpful?

Solution 2

I was able to fix this using the user_register hook. Annoyingly I can't find a wordpress hook that triggers after the user clicks the activation link and sets their password, so I don't think one exists.

wpmu_activate_user only works on Multisite installations which is most disappointing.

<?php function xxx($user_id) {
    
   //Get User Fields
    $user_data = get_userdata($user_id);
    $id = $user_data -> ID;
    $password = $user_data -> user_pass;

   //Get Custom Fields
    $first_name = get_field('first_name_c', 'user_'. $id );
    $last_name = get_field('last_name_c', 'user_'. $id );
    $address1 = get_field('address_1', 'user_'. $id );
    $city = get_field('city', 'user_'. $id );
    $state = get_field('state', 'user_'. $id );
    $postcode = get_field('postcode', 'user_'. $id );
    $country = get_field('country', 'user_'. $id );
    $phonenum = get_field('phone_number', 'user_'. $id );
    $email = $user_data -> user_email;
    
 <pruned rest of code>
    }

add_action('user_register',"xxx",10,1);

get_field() is from the Advanced Custom Field plugin.

OTHER TIPS

Change your hook to pass all 3 arguments to your xxx_whmcs_signup function:

add_action('wpmu_activate_user',"xxx_whmcs_signup", 10, 3);

At the moment it only passes a single argument - the $user_id rather than all 3 $user_id, $password and $meta.

Reference: wpmu_activate_user

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top