Email admin when post pending?

I have this little bit of code that sends me an email whenever a new posts is published by an author, but I want to be alerted to posts in ‘pending’ status by contributors too.

add_action('publish_post', 'send_admin_email');
function send_admin_email($post_id){
    $to = '[email protected]';
    $subject="New Post on The Articist";
    $message = "new post published at: ".get_permalink($post_id);
    wp_mail($to, $subject, $message );
}   

Can I have emails sent to me so I’m informed when posts are pending?

Code from userabuser’s first solution, this doesn’t seem to actually send emails(yes, I have changed it to my actual email address in the functions.php).

add_action( 'transition_post_status', 'pending_post_status', 10, 3 );

function pending_post_status( $new_status, $old_status, $post ) {

    if ( $new_status === "pending" ) {
        function send_pending_email($post_id){
            $to = '[email protected]';
            $subject="New Pending Post";
            $message = "new post Pending";

            wp_mail($to, $subject, $message );
           }

    }

}

2 Answers
2

There a couple of ways you can go about it depending on your use case and although example number 1 is perfectly valid, it might make more sense to employ the use of example number 2.

Example #1:

This will send you an email each time a post is updated/published with the status of “pending”. This will also email you if the status was already “pending” and the user hits save as pending again.

add_action( 'transition_post_status', 'pending_post_status', 10, 3 );

function pending_post_status( $new_status, $old_status, $post ) {

    if ( $new_status === "pending" ) {

        wp_mail(
            //your arguments here...
        );
    }

}

Example #2:

This will send you an email when a post is updated or published as pending only IF the previous status of the post was not already set to pending.

add_action( 'transition_post_status', 'pending_post_status', 10, 3 );

function pending_post_status( $new_status, $old_status, $post ) {

    if ( $new_status === "pending" && $old_status !== "pending" ) {

        wp_mail(
            //your arguments here...
        );
    }

}

Choose your poison…

Additional resources:

http://codex.wordpress.org/Post_Status_Transitions

Leave a Comment