admin管理员组

文章数量:1022964

I have a function that sends out emails using wp_mail() every time a post is published or updated. However, the content that is being sent is always one post behind.

If I update the content the email sends the previous content before the update. I am not sure how to fix. I appreciate any help you can give, thanks.

function send_media_emails($post_id){
    if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) 
        return;
    if(get_post_status($post_id) == 'draft' or get_post_status($post_id) == 'pending' or get_post_status($post_id) == 'trash')
        return;
    $to  = '[email protected]';
    $subject = 'My Email Subject';
    $post = get_post();

    $body = '<h1>'.$post->post_title.'</h1>';   
    if(is_category){
        $category = get_the_category();
        $body .= '<h2>Reason for Closure: <em>'.$category[0]->cat_name .'</em></h2>';
    }
    $body .= '<h3>This Cancellation/Delay was posted on '.$post->post_date.'</h3>';
    $body .= '<p>'.$post->post_content.'</p>';
    $body .= '<p>View this posting at ' . get_permalink($post_id) . ' or</p>';

    if(did_action('post_updated') == 1){
        wp_mail($to, $subject, $body);
    }
}
add_action('post_updated', 'send_media_emails');

I have a function that sends out emails using wp_mail() every time a post is published or updated. However, the content that is being sent is always one post behind.

If I update the content the email sends the previous content before the update. I am not sure how to fix. I appreciate any help you can give, thanks.

function send_media_emails($post_id){
    if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) 
        return;
    if(get_post_status($post_id) == 'draft' or get_post_status($post_id) == 'pending' or get_post_status($post_id) == 'trash')
        return;
    $to  = '[email protected]';
    $subject = 'My Email Subject';
    $post = get_post();

    $body = '<h1>'.$post->post_title.'</h1>';   
    if(is_category){
        $category = get_the_category();
        $body .= '<h2>Reason for Closure: <em>'.$category[0]->cat_name .'</em></h2>';
    }
    $body .= '<h3>This Cancellation/Delay was posted on '.$post->post_date.'</h3>';
    $body .= '<p>'.$post->post_content.'</p>';
    $body .= '<p>View this posting at ' . get_permalink($post_id) . ' or</p>';

    if(did_action('post_updated') == 1){
        wp_mail($to, $subject, $body);
    }
}
add_action('post_updated', 'send_media_emails');
Share Improve this question edited Apr 12, 2019 at 16:33 butlerblog 5,1313 gold badges28 silver badges44 bronze badges asked Feb 6, 2015 at 17:20 bilckerbilcker 14512 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 3

There's a few things you need to do here, you need to modify your add_action() to accept additional arguments and you need to specify whether to use the data before the update or the data after the update.

Try something like this:

function send_media_emails($post_id, $post_after){
    if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) 
        return;
    if(get_post_status($post_id) == 'draft' or get_post_status($post_id) == 'pending' or get_post_status($post_id) == 'trash')
        return;
    $to  = '[email protected]';
    $subject = 'My Email Subject';
    $post = $post_after; // NOTE We're using the after post update data here

    $body = '<h1>'.$post->post_title.'</h1>';   
    if(is_category){
        $category = get_the_category();
        $body .= '<h2>Reason for Closure: <em>'.$category[0]->cat_name .'</em></h2>';
    }
    $body .= '<h3>This Cancellation/Delay was posted on '.$post->post_date.'</h3>';
    $body .= '<p>'.$post->post_content.'</p>';
    $body .= '<p>View this posting at ' . get_permalink($post_id) . ' or</p>';

    if(did_action('post_updated') == 1){
        wp_mail($to, $subject, $body);
    }
}
add_action('post_updated', 'send_media_emails', 10, 2); 
// NOTE we've added 2 arguments to be accepted and also a priority of 10

More information on the add_action() arguments can be found here and you can see an example of the before and after post data on the post_updated action here

I have a function that sends out emails using wp_mail() every time a post is published or updated. However, the content that is being sent is always one post behind.

If I update the content the email sends the previous content before the update. I am not sure how to fix. I appreciate any help you can give, thanks.

function send_media_emails($post_id){
    if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) 
        return;
    if(get_post_status($post_id) == 'draft' or get_post_status($post_id) == 'pending' or get_post_status($post_id) == 'trash')
        return;
    $to  = '[email protected]';
    $subject = 'My Email Subject';
    $post = get_post();

    $body = '<h1>'.$post->post_title.'</h1>';   
    if(is_category){
        $category = get_the_category();
        $body .= '<h2>Reason for Closure: <em>'.$category[0]->cat_name .'</em></h2>';
    }
    $body .= '<h3>This Cancellation/Delay was posted on '.$post->post_date.'</h3>';
    $body .= '<p>'.$post->post_content.'</p>';
    $body .= '<p>View this posting at ' . get_permalink($post_id) . ' or</p>';

    if(did_action('post_updated') == 1){
        wp_mail($to, $subject, $body);
    }
}
add_action('post_updated', 'send_media_emails');

I have a function that sends out emails using wp_mail() every time a post is published or updated. However, the content that is being sent is always one post behind.

If I update the content the email sends the previous content before the update. I am not sure how to fix. I appreciate any help you can give, thanks.

function send_media_emails($post_id){
    if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) 
        return;
    if(get_post_status($post_id) == 'draft' or get_post_status($post_id) == 'pending' or get_post_status($post_id) == 'trash')
        return;
    $to  = '[email protected]';
    $subject = 'My Email Subject';
    $post = get_post();

    $body = '<h1>'.$post->post_title.'</h1>';   
    if(is_category){
        $category = get_the_category();
        $body .= '<h2>Reason for Closure: <em>'.$category[0]->cat_name .'</em></h2>';
    }
    $body .= '<h3>This Cancellation/Delay was posted on '.$post->post_date.'</h3>';
    $body .= '<p>'.$post->post_content.'</p>';
    $body .= '<p>View this posting at ' . get_permalink($post_id) . ' or</p>';

    if(did_action('post_updated') == 1){
        wp_mail($to, $subject, $body);
    }
}
add_action('post_updated', 'send_media_emails');
Share Improve this question edited Apr 12, 2019 at 16:33 butlerblog 5,1313 gold badges28 silver badges44 bronze badges asked Feb 6, 2015 at 17:20 bilckerbilcker 14512 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 3

There's a few things you need to do here, you need to modify your add_action() to accept additional arguments and you need to specify whether to use the data before the update or the data after the update.

Try something like this:

function send_media_emails($post_id, $post_after){
    if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) 
        return;
    if(get_post_status($post_id) == 'draft' or get_post_status($post_id) == 'pending' or get_post_status($post_id) == 'trash')
        return;
    $to  = '[email protected]';
    $subject = 'My Email Subject';
    $post = $post_after; // NOTE We're using the after post update data here

    $body = '<h1>'.$post->post_title.'</h1>';   
    if(is_category){
        $category = get_the_category();
        $body .= '<h2>Reason for Closure: <em>'.$category[0]->cat_name .'</em></h2>';
    }
    $body .= '<h3>This Cancellation/Delay was posted on '.$post->post_date.'</h3>';
    $body .= '<p>'.$post->post_content.'</p>';
    $body .= '<p>View this posting at ' . get_permalink($post_id) . ' or</p>';

    if(did_action('post_updated') == 1){
        wp_mail($to, $subject, $body);
    }
}
add_action('post_updated', 'send_media_emails', 10, 2); 
// NOTE we've added 2 arguments to be accepted and also a priority of 10

More information on the add_action() arguments can be found here and you can see an example of the before and after post data on the post_updated action here

本文标签: wp mailwpmail sending old content from post