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
1 Answer
Reset to default 3There'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
1 Answer
Reset to default 3There'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
版权声明:本文标题:wp mail - wp_mail sending old content from post 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745595468a2158140.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论