Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

So I manage a blog of about 15 contributors and 5 editors. I wanted to implement a system so that each time a contributor hits the 'Submit for Review' button, and email is sent to the editors to proof. I've cooked up an action, but it seemed to fire off at "edit post" time and not "submit" time.

My workaround was to put a check for the post status, only fire the email of it's "pending", but before they hit submit, the status is still 'draft'... this means it only works on the second they hit submit for review.

Here's my snippet so far. Please help!

function submit_send_email() {
    global $post;
    if ( current_user_can('contributor') && $post->post_status == 'pending' ) {
        $user_info = get_userdata ($post->post_author);
        $strTo = array ('email@example.com');
        $strSubject = 'Fstoppers: ' . $user_info->user_nicename . ' submitted a post';
        $strMessage = '"' . $post->post_title . '" by ' . $user_info->user_nicename . ' was submitted a post for review at ' . wp_get_shortlink ($post->ID) . '&preview=true. Please proof.';
        wp_mail( $strTo, $strSubject, $strMessage );
    }
}
add_action('edit_post','submit_send_email');

UPDATE: I tried to make Frankenstein setup, having my action schedule an event that would get run 15 seconds later, no dice.

function submit_send_email ($post) {
    if ( $post->post_status == 'pending' ) {
        $user_info = get_userdata ($post->post_author);
        $strTo = array ('email@example.com');
        $strSubject = 'Fstoppers: ' . $user_info->user_nicename . ' submitted a post';
        $strMessage = '"' . $post->post_title . '" by ' . $user_info->user_nicename . ' was submitted a post for review at ' . wp_get_shortlink ($post->ID) . '&preview=true. Please proof.';
        wp_mail( $strTo, $strSubject, $strMessage );
    }
}
function submit_for_review() {
    global $post;
    if ( current_user_can('contributor') ) {
        wp_schedule_single_event( time() + 15, 'submit_send_email_event', $post );
    }
}
add_action('submit_send_email_event','submit_send_email', 10, 1);
add_action('save_post','submit_for_review');
share|improve this question
    
I think you might want save_post as hook: codex.wordpress.org/Plugin_API/Action_Reference/save_post –  GhostToast Aug 27 '13 at 18:31
    
@GhostToast save_post resulted me the same thing :( –  aZn137 Aug 27 '13 at 20:12
    
Perhaps this may shed some light? wordpress.stackexchange.com/questions/16053/… –  GhostToast Aug 27 '13 at 21:03

2 Answers 2

up vote 4 down vote accepted

You need Post Status Transitions actions

function notify_me_for_pending( $post ) {
  $user_info = get_userdata ($post->post_author);
  $strTo = array ('email@example.com');
  $strSubject = 'Fstoppers: ' . $user_info->user_nicename . ' submitted a post';
  $strMessage = '"' . $post->post_title . '" by ' . $user_info->user_nicename . ' was submitted a post for review at ' . wp_get_shortlink ($post->ID) . '&preview=true. Please proof.';
  wp_mail( $strTo, $strSubject, $strMessage );
}

add_action( 'draft_to_pending', 'notify_me_for_pending' );
add_action( 'auto-draft_to_pending', 'notify_me_for_pending' );
share|improve this answer
    
This works PERFECTLY for me. Thank you very much! –  aZn137 Aug 28 '13 at 13:41

I am in a similar situation and the ugly method I came up with was to create a file with the following:

require('/path/to/yourdomain.com/httpdocs/wp-blog-header.php'); 
global $wpdb;

// Search for posts by Contributors then email the editors
$today = date('Y-m-d');
$two_days_ago = date('Y-m-d', strtotime("-1 days"));

// Select draft posts but don't include Site Admin/Editors to reduce un-necessary emails and also if they're in there for more than x days, assume that there's a reason that they're not published and ignore
$sql = "SELECT * FROM `wp_posts` WHERE `post_author` !=4 AND `post_author` !=1 AND `post_author` !=7 AND `post_author` !=10 AND `post_author` !=11 AND `post_author` !=12 AND `post_status` = 'pending' AND `post_modified` > '$two_days_ago' ";

$result = $wpdb->get_results($sql);
if ($wpdb->num_rows > 0) {
    $message_text = $wpdb->num_rows . " draft posts(s) for review. Please review here: http://yourdomain.com/wp-admin/edit.php?post_status=pending&post_type=post&orderby=date&order=desc";
    $headers = 'From: Site Admin <info@yourdomain.com>' . "\r\n";
    wp_mail('editors@yourdomain.com', 'Site Admin: Draft Posts awaiting approval', $message_text, $headers); 
    echo $wpdb->num_rows . " draft post(s) for approval";
    } else {
    echo " No posts for approval<br>";
    }

I then run a cron every 30 mins to see if there's any new posts. (I also have various other maintenance scripts running in here, etc.)

Thought this might help you.

share|improve this answer
    
Thanks, but I wanted a clean way of doing it, avoiding the messy crons :( –  aZn137 Aug 27 '13 at 20:13
    
I know what you mean - I may end up using whatever solution you find :) –  TomC Aug 28 '13 at 2:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.