this is not working!
For a recap, this is firstly for sync between wordpress+buddypress+x-profiles to mailchimp with reverse sync from mailchimp. Creating a user now also works, I am not sure about how the passwords work but I am not worried about that at all! Thank you Danny for all your help, you were and absolute lifesaver!
//Add buddypress xprofile field to MailChimp Sync
//Call the function
add_filter( 'mailchimp_sync_user_data', 'mailchimp_buddypress', 10, 2 );
//Write the function
function mailchimp_buddypress( $data, $user ) {
//Get user ID
$user_id = $user->ID;
// Store Xprofile fields in the $data variable
$data['STREET'] = xprofile_get_field_data( 13 , $user_id );
$data['STREET2'] = xprofile_get_field_data( 14 , $user_id );
$data['CITY'] = xprofile_get_field_data( 7 , $user_id );
$data['STATE'] = xprofile_get_field_data( 8 , $user_id );
$data['ZIP'] = xprofile_get_field_data( 9 , $user_id );
$data['PHONE1'] = xprofile_get_field_data( 16 , $user_id );
$data['PHONE2'] = xprofile_get_field_data( 17 , $user_id );
$data['COUNTRY'] = xprofile_get_field_data( 28 , $user_id );
$data['EMPLOYER'] = xprofile_get_field_data( 3 , $user_id );
$data['INDUSTRY'] = xprofile_get_field_data( 4 , $user_id );
$data['JOBTITLE'] = xprofile_get_field_data( 5 , $user_id );
$data['MAJOR'] = xprofile_get_field_data( 6 , $user_id );
$data['YEARSROWED'] = xprofile_get_field_data( 10 , $user_id );
$data['YEARROWED'] = xprofile_get_field_data( 11 , $user_id );
$data['COACH'] = xprofile_get_field_data( 18 , $user_id );
$data['ADVDEG'] = xprofile_get_field_data( 19 , $user_id );
//Return the data
return $data;
}
// hook into profile updates
add_action( 'mailchimp_sync_webhook_profile', function( $data, $user ) {
$fields = $data['merges'];
// mailchimp field names to buddypress field id's
$map = array(
'STREET' => 13,
'STREET2' => 14,
'CITY' => 7,
'STATE' => 8,
'ZIP' => 9,
'PHONE1' => 16,
'PHONE2' => 17,
'COUNTRY' => 28,
'EMPLOYER' => 3,
'INDUSTRY' => 4,
'YEARSROWED' => 10,
'YEARROWED' => 11,
'COACH' => 18,
'ADVDEG' => 19
);
foreach( $map as $mc_field => $bp_field_id ) {
if( ! empty( $fields[ $mc_field ] ) ) {
xprofile_set_field_data( $bp_field_id, $user->ID, $fields[ $mc_field ] );
}
}
}, 10, 2 );
/**
* This code hooks into the MailChimp User Sync webhook listener.
*
* When a request comes in that is not an unsubscribe request and for which no user exists yet, a new user will be created.
*/
add_filter( 'mailchimp_sync_webhook_user', function( $user, $data ) {
// have user already? use that.
if( $user instanceof \WP_User ) {
return $user;
}
// do not run when someone unsubscribed through MailChimp
if( $_REQUEST['type'] === 'unsubscribe' ) {
return $user;
}
// No user yet and this is not an unsubscribe request, let's create a new one!
//$user_id = wp_create_user( $data['email'], wp_generate_password(), $data['email'] );
$user_id = wp_create_user( $data['merges']['USERNAME'], wp_generate_password(), $data['email'] );
// send notification to user
wp_new_user_notification( $user_id );
// return complete user object
return get_userdata( $user_id );
}, 10, 2 );