Last active
December 29, 2021 11:23
-
-
Save verygoodplugins/e4090038cdec970667d142bc7ef0b413 to your computer and use it in GitHub Desktop.
Create / update a custom Event object in Zoho. Salesforce, or HubSpot when an "event" post type is created or updated
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// 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 ); |
Ok great. It should work then. But I'd suggest adding debug points at each line of the code so you can see where it's getting stuck.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi khuzemah,
We don't provide support for custom code.... just by looking at it, "Hund Name" and "Hund Owner" don't look like valid Zoho custom field IDs, since they have spaces in them. But I'm not really sure.
You can use error_log() or wpf_log() to debug the code at additional points. I would also make sure that your post type slug is
hund
, otherwise this code won't run when it's saved.If you're having trouble, we recommend the developers on Codeable