Custom function for “Submit for Review” hook

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.

Read More

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');

Related posts

2 comments

  1. 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' );
    
  2. 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>' . "rn";
        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.

Comments are closed.