How to use Post Status Transition in your Wordpress Plugins

Michael Phipps's picture

Bit of background.  A while ago, I wrote a plugin to send out an email to contributors when their post went from pending to published, to remind them to promote it. But recently, it stopped working. I was using code that looked a bit like this:

 

function mda_dn_transition_post_status($new_status, $old_status, $post) {
  if ('publish' == $new_status && !empty($post->ID) && in_array($post->post_type, array('post', 'page'))) {
  mda_publish_notifier($post->ID);
 } 
}
 
add_action('transition_post_status', 'mda_dn_transition_post_status', 10, 3);

 
But recently that code stopped working, because the value of $new_status was almost always inherit.  What I didn't know is that wordpress had introduced heaps of new actions
 

These actions are of the form status_to_status where status is one of:

  • 'new' - When there's no previous status
  • 'publish' - A published post or page
  • 'pending' - post in pending review
  • 'draft' - a post in draft status
  • 'auto-draft' - a newly created post, with no content
  • 'future' - a post to publish in the future
  • 'private' - not visible to users who are not logged in
  • 'inherit' - a revision. see get_children.
  • 'trash' - post is in trashbin. added with Version 2.9.

When a pending post is published, the action pending_to_publish is can be triggered. 

What isn't clear is how to use these actions in your plugin.

Well here it is:

add_action('pending_to_publish', 'your_function');

Where pending_to_publish is the action and your_function is the function to call when the action is triggered

So in this case, when a post is published from a pending status, the function will be run.

A very basic might look like this :

wp_login_plugin.php

 

SCHEDULED POSTS

If you want your function to trigger when a scheduled post is published you need to ALSO include

add_action('future_to_publish', 'your_function');

© 2010 Your Name.. Drupal theme by Kiwi Themes.