Post Pic

How to automatically email contributors when their posts are published

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!

12 Responses

Nov 10 2011 17:03

Interesting :) Can you trigger the function when a post i updated too?

Nov 10 2011 19:01

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

Nov 11 2011 04:56

Yeah, I think “global $wpdb” doesn’t need for this recipe :D

Nov 11 2011 16:02

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)

Nov 11 2011 16:13

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

Nov 13 2011 16:51

ooops, thank you guys! Post have been updated.

Dec 28 2011 04:10

Hmmm…
I think publish_post has deprecated for now :D

May 13 2012 19:22

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

Jul 06 2012 22:36

How can I add the URL to the Message?

Jul 06 2012 22:58

The code for the URL is:

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

Nov 28 2012 10:04

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 :)

Jan 28 2013 00:09

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

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required

WP Theme of the week

Sponsored Likebox