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'm trying to send an email only upon saving a post as a draft, this doesn't seem to work with the current code:

add_action( 'save_post', 'er_send_email_on_post_draft_save' );

function er_send_email_on_post_draft_save( $post_id ) {

    //verify post is not a revision
    if ( $post_id->post_status == 'draft' ) {

        $post_title = get_the_title( $post_id );
        $post_url = get_permalink( $post_id );
        $subject = 'A post has been updated';

        $message = "A post has been updated on your website:\n\n";
        $message .= "" .$post_title. "\n\n";

        //send email to admin
        wp_mail( 'myemail@undisclosed.co.uk', $subject, $message );

    }
}

it works if I change the if statement to:

!wp_is_post_revision( $post_id )

but that's not what I want, I only want to send notification if it's saved as a draft only.

share|improve this question
1  
Please head back to your old questions and accept some answer, its the least you can do to thank the people for their time they put in answering you question. AND ITS FREE! –  Bainternet Jul 3 '12 at 10:24
    
Excuse my ignorance, I'm not completely familiar with the system - what do I do, click the ticks? –  erichmond Jul 3 '12 at 11:07
    
Well, this will help you to familiarize: wordpress.stackexchange.com/faq –  brasofilo Jul 3 '12 at 11:44
    
@erichmond , yep clicking the ticks will accept the answers. –  Bainternet Jul 3 '12 at 12:41

1 Answer 1

up vote 2 down vote accepted

$post_id is an integer (just the post id) and not a post object (the whole post with id,status,title ...)

so globalize the $post object and check the status from there eg:

function er_send_email_on_post_draft_save( $post_id ) {
    global $post;
    //verify post is not a revision
    if ( $post->post_status == 'draft' ) {

        $post_title = get_the_title( $post_id );
        $post_url = get_permalink( $post_id );
        $subject = 'A post has been updated';

        $message = "A post has been updated on your website:\n\n";
        $message .= "" .$post_title. "\n\n";

        //send email to admin
        wp_mail( 'myemail@undisclosed.co.uk', $subject, $message );

    }
}

and if you want you can use a hook fired only when post is saved as draft

add_action('draft_post', 'send_my_mail_on_draft' );
function send_my_mail_on_draft( $post_id,$post){
   $post_title = get_the_title( $post_id );
   $post_url = get_permalink( $post_id );
   $subject = 'A post has been updated';

   $message = "A post has been updated on your website:\n\n";
   $message .= "" .$post_title. "\n\n";

   //send email to admin
   wp_mail( 'myemail@undisclosed.co.uk', $subject, $message );
}
share|improve this answer
    
Ahah! I looked on codex for an action like that but, I couldn't find it? Your suggestion would seem much better. –  erichmond Jul 3 '12 at 14:45
    
Glad you like it. –  Bainternet Jul 3 '12 at 15:33

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.