Skip to content

Instantly share code, notes, and snippets.

@jrmcdona
Created May 6, 2020 11:59
Show Gist options
  • Save jrmcdona/884d0ce8d78db9c12358fda9a278a20d to your computer and use it in GitHub Desktop.
Save jrmcdona/884d0ce8d78db9c12358fda9a278a20d to your computer and use it in GitHub Desktop.
add_action( 'save_post', 'save_default_post_hook', 10, 3);
function save_default_post_hook( $ID, $post, $update) {
// don't process our custom post types
if($post->post_type == "featurehighlight") {
return;
}
if ( 'auto-draft' === $post->post_status ) return;
if ( 'draft' === $post->post_status ) return;
if ( 'revision' === $post->post_type ) return;
if ( 'trash' === $post->post_type ) return;
if ( 'acf-field' === $post->post_type ) return;
if ( 'acf-field-group' === $post->post_type ) return;
$post_id = $ID;
$slug = get_page_uri($post_id);
$myObj = new stdClass();
$myObj->id = $post_id;
$myObj->slug = $slug;
$myObj->createdDate = new DateTime( $post->post_date_gmt );
$myObj->modifiedDate = new DateTime( $post->post_modified_gmt );
$myObj->title = $post->post_title;
$myObj->postType = $post->post_type;
$myObj->postStatus = $post->post_status;
$myObj->firstName = get_the_author_meta("first_name", $post->post_author); //https://developer.wordpress.org/reference/functions/get_the_author_meta/
$myObj->lastName = get_the_author_meta("last_name", $post->post_author);
$myObj->displayName = get_the_author_meta("display_name", $post->post_author);
$content = $post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
$myObj->content = $content;
$myObj->excerpt = $post->post_excerpt;
$myObj->permalink = $post->post_permalink;
//$myObj->featuredImage->mediaDetails->sizes = $post->featuredImage->mediaDetails->sizes;
$myObj->featuredImage = [];
// way 3 to get the category list (only category names)
foreach(get_the_category($ID) as $category) {
$category_names[] = $category->cat_name;
}
$myObj->categories = (object) $category_names;
foreach(get_the_tags($ID) as $tag) {
$tag_names[] = $tag->name;
}
$myObj->tags = (object) $tag_names; //implode(',', $tag_names);
//Hero or Feature aligned 1:1
$myObj->pageLayout = get_field('page_layout');
$myObj->blogFeedBadgeText = get_field('featured_badge');
$myObj->blogFeedImageAltText = get_field('image_alt_text');
$myObj->blogFeed_vp1_517x291 = get_field('article_feed_vp1_517x291');
//How do I guest guest author?
$is_new = $post->post_date === $post->post_modified;
$payload = [
'IsUpdate' => !$is_new,
'PostType' => $myObj->postType,
'Slug' => $myObj->slug, //We use slug here and not Uri
'Payload' => json_encode($myObj)
];
if(!$isProd) {
postToService($payload, $post->post_status);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment