WP_mail正在从帖子发送旧内容

时间:2015-02-06 作者:bilcker

我有一个发送电子邮件的功能,使用wp_mail() 每次发布或更新帖子时。然而,发送的内容总是落后一个帖子。

如果我更新了内容,电子邮件会在更新之前发送以前的内容。我不知道如何修复。我很感激你能给我的任何帮助,谢谢。

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\');

1 个回复
最合适的回答,由SO网友:Craig Pearson 整理而成

这里有几件事你需要做,你需要修改你的add_action() 要接受其他参数,您需要指定是在更新之前使用数据,还是在更新之后使用数据。

尝试以下操作:

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
有关add_action() 参数可以是found here 您可以在post_updated 行动here

结束

相关推荐