Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Assume I created a new post type 'product'. Is there a way to perform a function (user created) when this new 'product' post has been created?

share|improve this question
4  
@dotty: Do any of these answers below help you solve your problem? If so, please up vote them to thank the people who've helped you, and pick the best answer so your question doesn't remain in the "unanswered" list. –  MikeSchinkel Sep 10 '10 at 20:32

3 Answers 3

In functions.php:

function mynewproduct(){
    myfunction();
}
add_action( 'new_product', 'mynewproduct' );

For more and the official documentation, refer here:

http://codex.wordpress.org/Post_Status_Transitions

share|improve this answer

Hi @dotty:

Take a look at the end of the wp_insert_post() function in the file /wp-includes/post.php (on lines 2148 thru 2392 in WordPress 3.0.1. Note that WordPress uses this function both for adding and updating posts.)

At the end it has the following code. From this code you can identify the call to wp_transition_post_status() (more on that in a bit) and we have the action hooks edit_post, post_updated, save_post and wp_insert_post (frankly I don't know why we have the latter two instead of just one.) You can use any of those that are appropriate for your needs:

<?php
wp_transition_post_status($data['post_status'], $previous_status, $post);
if ( $update ) {
  do_action('edit_post', $post_ID, $post);
  $post_after = get_post($post_ID);
  do_action( 'post_updated', $post_ID, $post_after, $post_before);
}
do_action('save_post', $post_ID, $post);
do_action('wp_insert_post', $post_ID, $post);
return $post_ID;

And as @Jan Fabry mentioned there are the action hooks found in wp_transition_post_status() (on 2713 thru 2717 of /wp-includes/post.php in WordPress 3.0.1.) Note there are three of them; use as appropriate:

<?php
function wp_transition_post_status($new_status, $old_status, $post) {
  do_action('transition_post_status', $new_status, $old_status, $post);
  do_action("${old_status}_to_$new_status", $post);
  do_action("${new_status}_$post->post_type", $post->ID, $post);
}
share|improve this answer

The wp_transition_post_status function will be called, and this will trigger the action "${new_status}_$post->post_type", so if a product moves to the publish state, the action publish_product will be triggered.

share|improve this answer
    
So i assign my function to the publish_product action? –  dotty Sep 6 '10 at 13:48
    
@dotty: You can do that, but if a user creates a product but does not yet click "Publish", this hook will not be triggered. This may or may not be what you want. –  Jan Fabry Sep 6 '10 at 15:09

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.