2

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( '[email protected]', $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.

3
  • 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
    Commented Jul 3, 2012 at 10:24
  • Excuse my ignorance, I'm not completely familiar with the system - what do I do, click the ticks?
    – erichmond
    Commented Jul 3, 2012 at 11:07
  • Well, this will help you to familiarize: wordpress.stackexchange.com/faq
    – brasofilo
    Commented Jul 3, 2012 at 11:44

2 Answers 2

3

$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( '[email protected]', $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( '[email protected]', $subject, $message );
}
0
0

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

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.