Skip to content

Instantly share code, notes, and snippets.

@rmcfrazier
Created July 21, 2012 00:04
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 rmcfrazier/3153946 to your computer and use it in GitHub Desktop.
Save rmcfrazier/3153946 to your computer and use it in GitHub Desktop.
Use this class to create a plugin that will send an email when a post goes into pending review status. It should be noted that auto-save has to be turned off, if not multiple emails will be sent. The class handles turning off auto-save.
class PendingReviewNotifier
{
private $_email_sent_flag = false;
public function run()
{
// add notification action to save_post hook
add_action('save_post', array(&$this, 'send_pending_review_notice'));
// add disable autosave hook
add_action('wp_print_scripts', array(&$this, 'disableAutoSave'));
}
public function disableAutoSave(){
wp_deregister_script('autosave');
}
public function send_pending_review_notice($id) {
// the save_post hook is called twice on every post save, so I'm getting two emails
// I need to set a flag to check if an email has already been sent
if(empty($this->_email_sent_flag)) {
$post_status = null;
if(!empty($_POST['post_status'])) {
$post_status = $_POST['post_status'];
}
$user_id = null;
if(!empty($_POST['user_ID'])) {
$user_id = (integer)$_POST['user_ID'];
}
$user_data = null;
if(!empty($user_id) && is_int($user_id)) {
$user_data = get_userdata($user_id);
}
//verify post is not a revision
if ( !wp_is_post_revision( $post_id ) && !empty($post_status) && $post_status == 'pending') {
$message = 'A post was placed in pending review status on blog: ' . get_bloginfo('name').PHP_EOL.PHP_EOL;
$message .= "Post Title: " . $_POST['post_title'] . PHP_EOL.PHP_EOL;
// Choose one of the following options to show the author's name
$message .= "Post Author: " . $user_data->display_name . '('.$user_data->user_email.')'. PHP_EOL.PHP_EOL;
//$message .= "Link: " . get_permalink($id);
$subject = "Post in pending preview status: " .$_POST['post_title'];
$recipient = get_bloginfo('admin_email');
wp_mail( $recipient, $subject, $message );
// set the sent email flag
$this->_email_sent_flag = true;
}
}
}
}
// start
$pending_review_notifier = new PendingReviewNotifier();
$pending_review_notifier->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment