Skip to content

Instantly share code, notes, and snippets.

@atwellpub
Last active December 11, 2018 01:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atwellpub/85e52f2da70a457f19c0faff379e76cd to your computer and use it in GitHub Desktop.
Save atwellpub/85e52f2da70a457f19c0faff379e76cd to your computer and use it in GitHub Desktop.
Example of a Zapier Webhook listener that imports microblog entries into a CPT named 'news'
<?php
/**
* Zapier Webhook Listener - Creates a status update into the custom post type 'news' , tags it, categorizes it
*/
/* define your secret key - must match the webhook secret key you inputted into the Zapier Zap. */
$ztw_secretkey = 'supersecretkey';
/* hook action to the WordPress 'init' hook. This will process webhook requests from Zapier */
add_action('init' ,'ztw_webhook_create_status');
/* let's build the actions we should take when the webhook is accessed */
function ztw_webhook_create_status() {
/* lets only run actions when we're suposed to */
if (!isset($_GET['action']) || $_GET['action'] != 'create-custom-status' || !$_REQUEST['status_pubdate']) {
return;
}
/* let's load our secret key */
global $ztw_secretkey;
/* let's match the POST request's secret key to our secret key and exit if they do not match */
if ($_GET['token'] != $ztw_secretkey ) {
echo json_encode(array('error'=>true));
return;
}
/* create visually friendly date */
$date = new DateTime($_REQUEST['status_pubdate']);
/* import the title and limit it to 60 characters */
if (strlen( $_REQUEST['status_nohashtag_nolink']) > 60) {
$title = substr($_REQUEST['status_nohashtag_nolink'], 0, 60) . ' ...';
} else {
$title = $_REQUEST['status_nohashtag_nolink'];
}
/* build content without hashtags */
$status = $_REQUEST['status_nohashtag_nolink'] . "\r\n" . $_REQUEST['permalink'] ;
/* create new status */
$data = array (
'post_title'=> $title ,
'post_content'=> $status,
'post_status'=> 'publish',
'post_type'=> 'news' /* change this to your custom post type */
);
/* create the new post in the WordPress database */
$post_id = wp_insert_post($data);
/* set tags if they are available */
if (isset($_REQUEST['tags'])) {
wp_set_post_terms( $post_id , $_REQUEST['tags'] , 'news_tags', true );
}
/* set featured image if available - save it to the media gallery */
if (isset($_REQUEST['featured_image'])) {
ztw_generate_featured_image($_REQUEST['featured_image'] , $post_id , 'Auto generated via hudson-custom.php' );
}
/* Set Category Tag */
wp_set_post_terms( $post_id , $_REQUEST['channel'] , 'news_categories', true );
/* echo the post id back to Zapier and exit the script */
echo $post_id;
exit;
}
/**
* Downloads an image from the specified URL and attaches it to a post as a post thumbnail.
*
* @param string $file The URL of the image to download.
* @param int $post_id The post ID the post thumbnail is to be associated with.
* @param string $desc Optional. Description of the image.
* @return string|WP_Error Attachment ID, WP_Error object otherwise.
*/
function ztw_generate_featured_image( $file, $post_id, $desc ){
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
// Set variables for storage, fix file filename for query strings.
preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches );
if ( ! $matches ) {
return new WP_Error( 'image_sideload_failed', __( 'Invalid image URL' ) );
}
$file_array = array();
$file_array['name'] = basename( $matches[0] );
// Download file to temp location.
$file_array['tmp_name'] = download_url( $file );
// If error storing temporarily, return the error.
if ( is_wp_error( $file_array['tmp_name'] ) ) {
return $file_array['tmp_name'];
}
// Do the validation and storage stuff.
$id = media_handle_sideload( $file_array, $post_id, $desc );
// If error storing permanently, unlink.
if ( is_wp_error( $id ) ) {
@unlink( $file_array['tmp_name'] );
return $id;
}
return set_post_thumbnail( $post_id, $id );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment