Skip to content

Instantly share code, notes, and snippets.

@NateJLewis
Last active July 25, 2016 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NateJLewis/acde91c95dee21861c9f7ca00943ed34 to your computer and use it in GitHub Desktop.
Save NateJLewis/acde91c95dee21861c9f7ca00943ed34 to your computer and use it in GitHub Desktop.
Send email notifications via wp_mail when a post is submitted (pending review) then notify the post author that the post has been published.
<?php
function _pit_pending_post_status( $new_status, $old_status, $post ) {
$post_author_id = $post->post_author;
$post_author = get_the_author_meta( 'display_name', $post_author_id );
$post_date = $post->post_date;
$post_title = $post->post_title;
if ( $new_status === 'pending' && $old_status !== 'pending' ) {
$headers[] = 'Content-Type: text/html; charset=UTF-8';
$headers[] = 'From: PracticalIT <info@practicalit.info>' . "\r\n";
$admin_emails = array(
'your-admin-email1@practicalit.info',
'your-admin-email2@practicalit.info'
);
$subj = $post_title . ' is waiting review';
$body = '<b>' . $post_title . '</b> post by <em>' . $post_author . '</em>, submitted on <b>' . $post_date .'</b> is waiting review.';
wp_mail( $admin_emails, $subj, $body, $headers );
}
}
add_action( 'transition_post_status', '_pit_pending_post_status', 10, 3 );
function _pit_on_publish_pending_post( $post ) {
$headers[] = 'Content-Type: text/html; charset=UTF-8';
$headers[] = 'From: PracticalIT <info@practicalit.info>' . "\r\n";
$post_date = $post->post_date;
$post_title = $post->post_title;
$post_author_id = $post->post_author;
$user_email = get_the_author_meta( 'user_email', $post_author_id );
$subj = $post_title . 'has been approved';
$body = 'Your post - <b>' . $post_title . '</b> submitted on <b>' . $post_date .'</b> has been approved.';
wp_mail( $user_email, $subj, $body, $headers );
}
add_action( 'pending_to_publish', '_pit_on_publish_pending_post', 10, 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment