This is an extension of the post I wrote a while back that described how to perform an action whenever a post is published.
The action hook used in that post was for regular WordPress posts and looked like this:
1 2 3 4 5 6 7 8 | // function to be executed when a post is published function run_when_post_published() { // your function code here } add_action('new_to_publish', 'run_when_post_published'); add_action('draft_to_publish', 'run_when_post_published'); add_action('pending_to_publish', 'run_when_post_published'); |
Well, what about custom post types? Unfortunately, the hooks above don’t fire when a custom post type is published. However, thankfully, we have another nearly identical hook that does. I discussed it briefly on Pippin’s Pages. At that time I was only aware of the hook that allowed an action to run whenever a custom post type was published, which also fired anytime a custom post type was edited. The hook I used then looked like this:
1 2 | // replace {custom_post_type_name} with your post type name add_action('publish_{custom_post_type_name}', 'function_to_perform'); |
However, thanks to an insistent customer of mine, I was recently forced to dig a little deep and see if I could find a hook that works for custom post types just like the regular publish post action hook. The most important thing being that the action be run only the first time the post is published, and not anytime that it’s edited as well.
So here’s what we have.
1 2 3 4 5 6 7 8 9 10 | // function to be executed when a custom post type is published function run_when_post_published() { // your function code here } // replace {custom_post_type_name} with the name of your post type add_action('new_to_publish_{custom_post_type_name}', 'run_when_post_published'); add_action('draft_to_publish_{custom_post_type_name}', 'run_when_post_published'); add_action('pending_to_publish_{custom_post_type_name}', 'run_when_post_published'); |
Using the combination of these three hooks, we can perform any action we wish anytime a custom post type is published.
To see it in a real world example, check out my Post Notify plugin from Code Canyon.net. For that plugin, I used action hooks just like these to send out, or schedule, emails to a list of subscribers announcing that a new post has been published.
Enjoy!






Once I initially commented I clicked the -Notify me when new comments are added- checkbox and now each time a remark is added I get 4 emails with the same comment. Is there any manner you may take away me from that service? Thanks!
- spam
- offensive
- disagree
- off topic
Like