1

I have a custom post type set up so that a user in the role of contributor can create a post, but an administrator has to approve the post and publish it.

I'd like to hook into the event when the administrator publishes this post type and then send an email to all contributors that there has been a post.

I tried a test email to myself hooking off of 'save_post' when publishing a post and it worked, but I got sent five emails. There's no loop in the function:

add_action( 'save_post', 'clr_test_email' );

function clr_test_email($post_id) {

   mail("[email protected]","
   ok hooked into save post","Testing save post trigger".$post_id,"From: [email protected]\r\nContent-Type: text/plain;charset=utf-8");

}

So I'm not sure if 'save_post' is my best bet.

There used to be a 'publish_post' hook, but according to the Codex it has been deprecated.

Any thoughts?

3
  • Have you tried the draft_to_publish hook or the pending_to_publish hook? See relevant codex entry: codex.wordpress.org/Post_Status_Transitions Jul 10, 2014 at 16:14
  • Thanks very much, I'm going to try those and post back here with the result. Jul 10, 2014 at 16:20
  • Thanks nothingtosee, "pending_to_publish" was the ticket! How do I give your rep a bump? Jul 11, 2014 at 18:20

1 Answer 1

1

When hooking on to save_post you need to check that it's not a revision & is not WP autosaving the data..

You'll need this code before you send the email so it returns if it's autosaving or is a post revision.

if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
    return;
}
if ( wp_is_post_revision( $post_id ) ) {
    return;
}

The post revision one is the first example on this page: http://codex.wordpress.org/Plugin_API/Action_Reference/save_post

1
  • Where should i put this code? I mean witch file? I also see the link you mentioned but could not find a way to implementation
    – Mahdi
    Apr 15, 2018 at 13:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.