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 ); }