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.
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.
A concise post about one of the most important WordPress hooks. Features a handy function to help you get started.