WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a function on my site that it supposed to run when a new post is published (email push notification), with the exception of the same article updated (i.e publish_to_publish transition, I don't want to send unnecessary push notifications)

I know that I can use the {old_status} to {new_status} action hooks, but this means that I have to specify all the transitions to publish (new_to_publish, draft_to_publish, etc.).

My question is:

  1. Can I use instead the publish_post hook and detect if it was publish_to_publish so I can explicitly negate it? something like:

    function send_email() {
     if ($transition == 'publish_to_publish') return;
      //(else) send email
    }
      add_action('publish_post', 'send_email', 10, 1);
    
  2. If not, how do I tie multiple hooks to the same action? Do I just list them like so:

    add_action('new_to_publish', 'send_mail', 10, 1);
    add_action('future_to_publish', 'send_mail', 10, 1);
    add_action('draft_to_publish', 'send_mail', 10, 1);
    

Or is there a more elegant way - like passing an array?

share|improve this question
up vote 2 down vote accepted

If not, how do I tie multiple hooks to the same action? Do I just list them like so:

add_action('new_to_publish', 'save_new_post', 10, 1);
add_action('future_to_publish', 'save_new_post', 10, 1);
add_action('draft_to_publish', 'save_new_post', 10, 1);

This is exactly the way to go. Just hook the same callback into each of the status-transition hooks on which you want the callback to fire.

share|improve this answer

We can do this is one hook by using the transition_post_status. All we need to do is to check that the old status is not publish. We can also exclude other statuses by creating an array of these statuses and check against $old_status

Note, this requires PHP 5.4+ and is untested

add_action( 'transition_post_status', function ( $new_status, $old_status, $post )
{
    // First check if our new status is publish before we do any work
    if ( 'publish' !== $new_status )
        return;

    // Create the array of post statuses to check against
    $statuses = ['publish', 'pending', 'private', 'trash'];

    // If the old status is an the $statuses array, bail
    if ( in_array( $old_status, $statuses ) )
        return;

    // Everything checked out, lets do what we are suppose to do
    // send email
}, 10, 3 );
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.