WordPress Publish Post Hook

By  on  

One of the best parts of WordPress is its hook/action system; this special hook system is WordPress' way of assigning callbacks when certain events occur. One event that there seems to be a lot of confusion over is which hook to use to detect when a post is initially published. There's the publish_post hook but that fires when you click the "Update" button after a post has already been published; that's not ideal.

Scour the WordPress documentation and forums and you're sure to see a dozen other solutions but none work as well as the transition_post_status hook:

// Add the hook action
add_action('transition_post_status', 'send_new_post', 10, 3);

// Listen for publishing of a new post
function send_new_post($new_status, $old_status, $post) {
  if('publish' === $new_status && 'publish' !== $old_status && $post->post_type === 'post') {
    // Do something!
  }
}

The transition_post_status occurs when a post goes from one status to another; you can check out the post status list to see other possible values. I've also added a post_type check to ensure the post is a blog post and not a page.

Whew, took me a while to find what I needed here -- hopefully this saves you a lot of searching and pain!

Recent Features

  • By
    Responsive Images: The Ultimate Guide

    Chances are that any Web designers using our Ghostlab browser testing app, which allows seamless testing across all devices simultaneously, will have worked with responsive design in some shape or form. And as today's websites and devices become ever more varied, a plethora of responsive images...

  • By
    Serving Fonts from CDN

    For maximum performance, we all know we must put our assets on CDN (another domain).  Along with those assets are custom web fonts.  Unfortunately custom web fonts via CDN (or any cross-domain font request) don't work in Firefox or Internet Explorer (correctly so, by spec) though...

Incredible Demos

  • By
    Fading Links Using jQuery:  dwFadingLinks

    UPDATE: The jQuery website was down today which caused some issues with my example. I've made everything local and now the example works. Earlier this week, I posted a MooTools script that faded links to and from a color during the mouseover and mouseout events.

  • By
    MooTools 1.2 Tooltips: Customize Your Tips

    I've never met a person that is "ehhhh" about XHTML/javascript tooltips; people seem to love them or hate them. I'm on the love side of things. Tooltips give you a bit more information about something than just the element itself (usually...

Discussion

  1. Manny Fleurmond

    Alternatives are {$old_status}_to_{$new_status}, which takes one argument of the post in question and is a bit more precise and {$new_status}_{post_type}

  2. Well if a post is already published and we want to edit in such a way that its automatically go in the recent one !! then what is the best procedure for it ?

  3. X-R

    Hi !
    THanks for the tips very useful !
    unfortunatly, I can’t get this work,
    it seems that the hook is only called when created a new post,

    even with simple

    function intercept_all_status_changes( $new_status, $old_status, $post ) {
        d($new_status);
    }  
    add_action( 'transition_post_status', 'intercept_all_status_changes', 10, 3 );

    Do you confirm it works for you ?

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!