Wordpress- 'publish_post' is also triggered on subsequent post editions

10 years ago

If you ever dug into the wordpress plugin world you know that you can create some hooks on the wordpress flow to allow execution of your functions when something interesting happens. One of those hooks is the 'publish_post', which will be triggered when the post is published and - surprise - also everytime the post is edited and it's status is published.

I recently had to dwell into this because I wanted my blog to post to twitter (and other sites) *only* when the post was actually published. I.e.: I don't want to spam people everytime I update a post.

So it turns out there are some other hooks you can set instead, that will do the trick, namely:

add_action('draft_to_publish', 'example'); add_action('pending_to_publish', 'example');

The first one will run function 'example' (with the post_id as the single argument, by the way) when a post transitions from the draft to the published status. The second will do the same on the pending to published transition.

No clue why the wordpress folks made it so the 'publish_post' hook also triggers when the posts are updated, but they must have their reasons :)