Skip to main content
Yoosuf Mohamed

Engineering

WordPress: How to trigger the Post publish event

How to hook into and trigger the WordPress post-publish event for custom automation.

1 min read

By Yoosuf Mohamed, Systems Architect

These days I’m working on a news publishing app, focused on its back-end API. Now that we’re in the final stage of development, we’ve started adding push notifications, so I needed a way to trigger an event the moment a WordPress post is published, in order to send push notifications to Android and iOS devices. While experimenting with WordPress functions, I came up with this solution, which is handy, and I believe it’ll be useful for anyone wanting to build a similar feature.

By the way, if you know a better way to handle this, please let me know. Improvements are always welcome for apps dealing with millions of users, right?

         /*
 * Add this function somewhere in functions.php
 * You will see there is nothing is magic in WordPress
**/

function on_all_status_transitions( $new_status, $old_status, $post ) {
	if ('publish' == $new_status && 'publish' != $old_status && 'trash' != $old_status) {

	// Your code comes here

  }
}
add_action(  'transition_post_status',  'on_all_status_transitions', 10, 3 );              

The gist is still at https://gist.github.com/yoosuf/3890f3789754afe65525 .

  • WordPress
  • WordPress Hooks
  • PHP
  • Automation

Let's build something that scales.

Planning an AI product, automating a workflow, or taking a platform to production? I'd love to hear what you're building.