3

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

4

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

2
  • Hi userabuser, I edited my first post to reflect your answer. (I can't get it to work, I'm sure I'm doing it right though).
    – andy
    Commented Jun 27, 2013 at 16:17
  • ...actually you are doing it wrong, what you have done is nested a function within a function BUT then you fail to call your nested function send_pending_email so while the conditional statement if ( $new_status === "pending" ) is evaluating to TRUE, nothing happens because send_pending_email doesn't get called. Remove the inner function send_pending_email as it's not needed.
    – Adam
    Commented Jun 28, 2013 at 2:14
1

WordPress has a hook transition_post_status that might help. Take a look at http://blog.joshschumacher.com/2011/10/11/wordpress-transition_post_status-action/ for documentation on its usage.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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