DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

If you’re running a multi-authored blog, it can be very cool to let your contributors know when their post are online. Today’s recipe will show you how to do this automatically each time a post is published.

Nothing complicated with this recipe. Copy the code below and paste it on your functions.php file. Then save the file, and you’re done!

function wpr_authorNotification($post_id) {
   $post = get_post($post_id);
   $author = get_userdata($post->post_author);

   $message = "
      Hi ".$author->display_name.",
      Your post, ".$post->post_title." has just been published. Well done!
   ";
   wp_mail($author->user_email, "Your article is online", $message);
}
add_action('publish_post', 'wpr_authorNotification');

Thanks to Daniel Pataki for the great piece of code!

Tags

12 Comments

  1. Interesting 🙂 Can you trigger the function when a post i updated too?

  2. I don’t think there is a need for the global $wpdb;.

  3. Yeah, I think “global $wpdb” doesn’t need for this recipe 😀

  4. I was going to say the same thing about globalizing the db class. Also, the name of the function does not match the callback. (authorNotification != wpr_authorNotification)

  5. Also, you are prefixing the function with wpr_ but you forgot to add that prefix in the action you’re adding to publish_post 😉

  6. ooops, thank you guys! Post have been updated.

  7. Hmmm…
    I think publish_post has deprecated for now 😀

  8. thanks
    but , how can i add post url to my massage?

  9. How can I add the URL to the Message?

  10. The code for the URL is:

    URL: http://www.domain.com/?p=“.$post->ID.”

  11. Hi,

    Is there a way to add an user to this code ? I’d like my Community Manager to receive the same email for every published post.

    Thanks in advance 🙂

  12. Mylene,
    I would assume that you could edit the ” wp_mail($author->user_email, “Your article is online”, $message);
    ” portion, but I’m not an expert.

    $author could be $admin

Leave a Comment

Your email address will not be published. Required fields are marked *