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.

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 = 'myemail@email.com';
    $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 = 'myemailaddresshere@mysite.com';
            $subject = 'New Pending Post';
            $message = "new post Pending";

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

    }

}
share|improve this question

2 Answers 2

up vote 2 down vote accepted

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

share|improve this answer
    
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 Jun 27 '13 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. –  userabuser Jun 28 '13 at 2:14

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.

share|improve this answer

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.