butlerblog
9/3/2014 - 10:56 PM

Insert a new user to MailPoet and handle custom fields

Insert a new user to MailPoet and handle custom fields

<?php
// The ID of the lists you want to add a user to
$list_ids = array( 1 );

// User Data without the custom fields
$data_subscriber = array(
	'user' => array(
		'email' => 'johndoe@email.com',
		'firstname' => 'John',
		'lastname' => 'Doe',
	),
	'user_list' => array(
		'list_ids' => $list_ids
	),
);

// Insert the user to the database and return it's ID
$user_id = WYSIJA::get( 'user', 'helper' )->addSubscriber( $data_subscriber );

/**
 * Possible results for $user_id
 * (bool) true = User Already Exists
 * (bool) false = There was an error
 * (int) = The ID of the user on MailPoet's Table
 */

/**
 * Here you will handle all the Custom Fields
 */
if ( is_numeric( $user_id ) ){
	WJ_FieldHandler::handle_all(
		array(
			'cf_3' => 'Google' // Company Field on this exemple
		),
		$user_id
	);
}