WordPress Snippets at WPcustoms

Notify author when post has been published

An email is sent to the post’s author if the post is published. That’s a nice function if you offer a frontend post submission form and auto set submitted posts to pending. Once you approve the post, the post author will be informed that his/her post has been published on your site.


/**
 * Snippet Name: Notify author when post has been published
 * Snippet URL: https://wpcustoms.net/snippets/notify-author-when-post-has-been-published/
 */
  function wpc_send_author_email_when_published($post_id){
    // get the post's author ID
    $post_author_id = get_post_field( 'post_author', $post_id );
    //get e-mail address from post meta field using our post_author_id
    $email_address = get_the_author_meta('user_email', $post_author_id);


    $subject = 'Your post has been published.';
    $body = 'Thank you for your submission!';

    wp_mail($email_address, $subject, $body,);
}
add_action('publish_post','wpc_send_author_email_when_published');