add_object()

Overview

This helper function is available with Ontraport, Zoho, Salesforce, and other CRMs which use custom objects.

It’s a shortcut / alternative to using the wpf_crm_object_type filter, for cases where you just need to add a custom object one time, without changing the object type used for WP Fusion globally.

Parameters

  • $data: (array) An associative array of data to sync to the CRM, using CRM field IDs for the keys
  • $object_type: (string) The object type you wish to update

Examples

Create a new Lead in Salesforce

$data = array(
	'Email'     => '[email protected]',
	'FirstName' => 'Jane',
	'LastName'  => 'Doe',
);

$lead_id = wp_fusion()->crm->add_object( $data, 'Lead' );

Update an Event object in Zoho when an Event post type is updated in WordPress

// Runs on any post with post type "event" and updates the "Event" custom object with values Title and EventDate

function create_update_event_object( $post_id, $post, $update ) {
	
	// Don't run if WP Fusion isn't active, otherwise you'll get an error

	if ( ! function_exists( 'wp_fusion' ) ) {
		return;
	}
	
	// This is the data to be sent to the CRM

	$event_data = array(
		'Title'     => $post->post_title,
		'EventDate' => get_post_meta( $post_id, 'event_date', true )
	);
	
	// See if this object has already been synced
	
	$object_id = get_post_meta( $post_id, wp_fusion()->crm->slug . '_event_id', true );

	if ( empty( $object_id ) {

		// New event

		$object_id = wp_fusion()->crm->add_object( $event_data, 'Event' );

		if ( ! is_wp_error( $object_id ) ) {
			
			// Save the ID of the new record for future updates.
			
			update_post_meta( $post_id, wp_fusion()->crm->slug . '_event_id', $object_id );

		} else {
			
			// Error, log it.
		
			wpf_log( 'error', 0, 'Error creating event:' . $object_id->get_error_message() );
			
		}

	} else {

		// Existing event

		wp_fusion()->crm->update_object( $object_id, $event_data, 'Event' );

	}

}
	    
// save_post_event runs whenever an "event" post type is created or updated (see https://developer.wordpress.org/reference/hooks/save_post_post-post_type/)

add_action( 'save_post_event', 'create_update_event_object', 10, 3 );

Was this helpful?