Email authors when their articles are published

When you’re an admin or an author with the privilege to publish his own posts, you don’t need any extra notification about it – you’ve just published your own post, right? But if you’re an author/contributor without those privileges and you’re not logged in to the WordPress admin all the time so you can’t see the status of your posts, it would be nice to receive a message when your post finally goes live.

Instead of thinking about sending a private message to an author whose text has been approved or proof-read, you can automatize the whole process.

There are numberless plugins which can help you control that, but in this article we are about to show you a simple function which will send emails for you.

Without any extra conditions, let’s create a simple function which will find the author of the post, his email, post title and send the email after his/her post has been published:

  1. Open functions.php
  2. Copy and paste the code:
  3. function wpr_authorNotification($post_id) {
    $post = get_post($post_id);
    $author = get_userdata($post->post_author);
    $message = "
    Hey there ".$author->display_name.",
    Your post, ".$post->post_title." has just been published. You can now visit your own text and enjoy it embedded into the site!";
    wp_mail($author->user_email, "Your article has been published", $message);
    }
    
    add_action('publish_post', 'wpr_authorNotification');
  4. Change the text of the message to your needs
  5. Save changes

If you don’t want everyone to be notified about the post, you can check for the user role and execute the code only if, for example, the post author has a Contributor role or a specific username.

For example, you can send email notifying user about the post only if that user is John:

if ( $author='John' ) {
add_action('publish_post', 'wpr_authorNotification');
}

If you want more control over notifying your users, see how to automatically notify members of your new posts by using a plugin. Along that, you can let WordPress notify you about any post changes.

START YOUR OWN BLOG

This guide is an introduction to mastering the art of blogging. It provides easy to follow steps to start, maintain, and grow your blog.

Read the guide

Leave a Reply

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