{"id":6460,"date":"2019-10-15T16:13:39","date_gmt":"2019-10-15T10:43:39","guid":{"rendered":"https:\/\/codeforgeek.com\/?p=6460"},"modified":"2023-12-22T19:05:41","modified_gmt":"2023-12-22T13:35:41","slug":"wordpress-publish-post-hook","status":"publish","type":"post","link":"https:\/\/codeforgeek.com\/wordpress-publish-post-hook\/","title":{"rendered":"WordPress Publish Post Hook"},"content":{"rendered":"<p>As I have mentioned in our technology stack article, we use WordPress to manage our content. Our front-end is handled and served using Node and MongoDB stack and I needed to connect these two systems (WordPress and custom Node stack) without making any large dependencies. <\/p>\n<p>One of the old school ways is to create a <strong>job scheduler<\/strong> which looks for changes in the WordPress periodically and create\/update content in local MongoDB.<\/p>\n<p>I don&#8217;t like this approach at all. Instead, I was looking for something event-based i.e when we publish a new article or update the article, WordPress generates an event and allows us to write our business logic after the event.<\/p>\n<p>I found <strong>WordPress publish hooks<\/strong> to be the solution.<\/p>\n<p>Here is my live WordPress publish hook. This code should be placed in the <strong>functions.php<\/strong>.<\/p>\n<p><code lang='php'><br \/>\n\/\/ add action<br \/>\nadd_action( 'post_updated', 'post_update_function', 10, 3 );<\/p>\n<p>function post_update_function( $post_ID, $post_after, $post_before ) {<br \/>\n\t\/\/ on each change, call this api to notify the content engine<br \/>\n\t$event = null;<br \/>\n\t\/\/ if article is auto draft<br \/>\n\tif($post_before -> post_status === 'auto-draft' && $post_after -> post_status === 'publish') {<br \/>\n\t\t$event = 'new_article';<br \/>\n\t\/\/ if article is draft then published<br \/>\n\t} else if($post_before -> post_status === 'draft' && $post_after -> post_status === 'publish') {<br \/>\n\t\t$event = 'new_article';<br \/>\n\t\/\/ if its updated article<br \/>\n\t} else if($post_before -> post_status === 'publish' && $post_after -> post_status === 'publish') {<br \/>\n\t\t$event = 'updated_article';<br \/>\n\t} else {<br \/>\n\t\treturn;<br \/>\n\t}<br \/>\n\tif($event === null) {<br \/>\n\t\treturn;<br \/>\n\t}<br \/>\n\t$response = wp_remote_post('----custom API end point ------', array(<br \/>\n\t\t'method' => 'POST',<br \/>\n\t\t'body' => array('eventType' => $event, 'postId' => $post_ID )<br \/>\n\t));<br \/>\n}<br \/>\n<\/code><\/p>\n<p>As you can see in the code shown above, whenever we create or update a post, a new event will be triggered that will in turn call <strong>post_update_function<\/strong> function.<\/p>\n<p>You can write any custom business logic inside the function. I wrote a code that calls the API exposed by the Node server. <\/p>\n<p>I have also wrapped this in a nice and clean WordPress plugin. Click <a href=\"https:\/\/github.com\/codeforgeek\/wp-event-generator-plugin\" rel=\"noopener noreferrer\" target=\"_blank\">here<\/a> to visit the repository.<\/p>\n<h3>Further study<\/h3>\n<ul>\n<li><a href=\"https:\/\/wordpress.org\/support\/article\/post-status\/\" rel=\"noopener noreferrer\" target=\"_blank\">WordPress post status.<\/a><\/li>\n<li><a href=\"https:\/\/codex.wordpress.org\/Plugin_API\" rel=\"noopener noreferrer\" target=\"_blank\">WordPress Hooks and filters.<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>As I have mentioned in our technology stack article, we use WordPress to manage our content. Our front-end is handled and served using Node and MongoDB stack and I needed to connect these two systems (WordPress and custom Node stack) without making any large dependencies. One of the old school ways is to create a [&hellip;]<\/p>\n","protected":false},"author":69,"featured_media":6461,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_surecart_dashboard_logo_width":"180px","_surecart_dashboard_show_logo":true,"_surecart_dashboard_navigation_orders":true,"_surecart_dashboard_navigation_invoices":true,"_surecart_dashboard_navigation_subscriptions":true,"_surecart_dashboard_navigation_downloads":true,"_surecart_dashboard_navigation_billing":true,"_surecart_dashboard_navigation_account":true,"_uag_custom_page_level_css":"","footnotes":""},"categories":[16,86],"tags":[],"class_list":["post-6460","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-short-tips","category-wordpress"],"blocksy_meta":[],"uagb_featured_image_src":{"full":["https:\/\/codeforgeek.com\/wp-content\/uploads\/black-and-white-black-and-white-chrome-1256916.jpg",1280,853,false],"thumbnail":["https:\/\/codeforgeek.com\/wp-content\/uploads\/black-and-white-black-and-white-chrome-1256916-150x150.jpg",150,150,true],"medium":["https:\/\/codeforgeek.com\/wp-content\/uploads\/black-and-white-black-and-white-chrome-1256916-300x200.jpg",300,200,true],"medium_large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/black-and-white-black-and-white-chrome-1256916-768x512.jpg",768,512,true],"large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/black-and-white-black-and-white-chrome-1256916-1024x682.jpg",1024,682,true],"1536x1536":["https:\/\/codeforgeek.com\/wp-content\/uploads\/black-and-white-black-and-white-chrome-1256916.jpg",1280,853,false],"2048x2048":["https:\/\/codeforgeek.com\/wp-content\/uploads\/black-and-white-black-and-white-chrome-1256916.jpg",1280,853,false]},"uagb_author_info":{"display_name":"Pankaj Kumar","author_link":"https:\/\/codeforgeek.com\/author\/pankaj\/"},"uagb_comment_info":0,"uagb_excerpt":"As I have mentioned in our technology stack article, we use WordPress to manage our content. Our front-end is handled and served using Node and MongoDB stack and I needed to connect these two systems (WordPress and custom Node stack) without making any large dependencies. One of the old school ways is to create a&hellip;","_links":{"self":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/6460","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/users\/69"}],"replies":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/comments?post=6460"}],"version-history":[{"count":0,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/6460\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media\/6461"}],"wp:attachment":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media?parent=6460"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/categories?post=6460"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/tags?post=6460"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}