Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Sync a 'Job' CPT item to Customer.io Collections whenever it is published or updated.
- *
- * @package my_wp_fusion_customizations
- */
- /**
- * Sends Job CPT data to Customer.io Collections on save_post.
- *
- * @param int $post_id ID of the post being saved.
- * @param WP_Post $post The post object.
- * @param bool $update Whether this is an existing post being updated or not.
- */
- function my_prefix_sync_job_to_customer_io_collections( $post_id, $post, $update ) {
- // Only run for our 'job' CPT.
- if ( 'job' !== $post->post_type ) {
- return;
- }
- // If this is an autosave or a revision, exit.
- if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
- return;
- }
- // Replace with your real Customer.io API key.
- $customer_io_api_key = 'YOUR_CUSTOMER_IO_API_KEY';
- // Build the data you want to send to Customer.io.
- // Expand or modify these fields as needed.
- $data = array(
- 'id' => (string) $post_id, // Unique identifier in the collection.
- 'title' => sanitize_text_field( $post->post_title ),
- 'link' => esc_url_raw( get_permalink( $post_id ) ),
- // Add meta fields, e.g.:
- // 'skills_required' => sanitize_text_field( get_post_meta( $post_id, 'skills_required', true ) ),
- // etc.
- );
- // Prepare the remote request arguments.
- // For the Collections API you may need POST, PUT, or PATCH.
- $args = array(
- 'method' => 'PUT', // or 'POST', depending on whether you want to upsert or create.
- 'headers' => array(
- 'Authorization' => 'Bearer ' . $customer_io_api_key,
- 'Content-Type' => 'application/json',
- ),
- 'body' => wp_json_encode( $data ),
- );
- // Adjust the endpoint with your real Collection ID in Customer.io:
- $collection_id = 'YOUR_COLLECTION_ID';
- // Typically an upsert endpoint might look like:
- $endpoint_url = 'https://track.customer.io/api/v1/collections/' . $collection_id . '/items/' . $post_id;
- // Send the request.
- $response = wp_remote_request( $endpoint_url, $args );
- // Optional: check for errors.
- if ( is_wp_error( $response ) ) {
- error_log(
- sprintf(
- /* translators: %s is the WP_Error message */
- esc_html__( 'Customer.io Collection sync error: %s', 'wp-fusion' ),
- $response->get_error_message()
- )
- );
- } else {
- error_log(
- esc_html__( 'Job synced successfully to Customer.io Collections.', 'wp-fusion' )
- );
- }
- }
- add_action( 'save_post', 'my_prefix_sync_job_to_customer_io_collections', 10, 3 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement