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("myemail@gmail.com","
   ok hooked into save post","Testing save post trigger".$post_id,"From: crou@zzz.com\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?

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

  • 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 – Kenji Apr 15 '18 at 13:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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