WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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

Try with this:

function dddn_process($id) {

    // emails anyone on or above this level
    $email_user_level = 7;

    global $wpdb;

    $tp = $wpdb->prefix;

    $result = $wpdb->get_row("
        SELECT post_status, post_title, user_login, user_nicename, display_name 
        FROM {$tp}posts, {$tp}users 
        WHERE {$tp}posts.post_author = {$tp}users.ID 
        AND {$tp}posts.ID = '$id'
    ");

    if (($result->post_status == "draft") || ($result->post_status == "pending")) {

        $message = "";
        $message .= "Draft updated on '" . get_bloginfo('name') . "'\n\n";
        $message .= "Title: " . $result->post_title . "\n\n";

            // *** Choose one of the following options to show the author's name

        $message .= "Author: " . $result->display_name . "\n\n";
        // $message .= "Author: " . $result->user_nicename . "\n\n";
        // $message .= "Author: " . $result->user_login . "\n\n";

        $message .= "Link: " . get_permalink($id);

        $subject = "Draft updated on '" . get_bloginfo('name') . "'";


        $editors = $wpdb->get_results("SELECT user_id FROM {$tp}usermeta WHERE {$tp}usermeta.meta_value >= " . $email_user_level);

        $recipient = "";    

        foreach ($editors as $editor) {         
            $user_info = get_userdata($editor->user_id);
            $recipient .= $user_info->user_email . ','; 
        } 

        mail($recipient, $subject, $message);


    }

}


add_action('save_post', 'dddn_process');

?>

Just create notification.php file with wp plugins description upload it to plugins folder and activate as regular plugin

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.