admin管理员组文章数量:1333451
I have a custom post type immobile
with some custom fields created with Toolset Types.
I need to send and email with the title and a custom field when a new immobile
is saved as a draft or it's published.
I managed to do almost everthing. My code is like this:
add_action( 'auto-draft_to_draft', 'my_send_email', 99 );
add_action( 'auto-draft_to_publish', 'my_send_email', 99 );
add_action( 'draft_to_publish', 'my_send_email', 99 );
function my_send_email( $post ) {
if ( $post->post_type == 'immobile' ) {
wp_mail(
'[email protected]',
'New immobile saved as ' . $post->post_status,
'Title: ' . $post->post_title . '. Ref: ' . get_post_meta( $post->ID, 'wpcf-reference', true ),
array('Content-Type: text/html; charset=UTF-8')
);
}
}
Unfortunately my custom field is not showing in the email, probably because It get saved after the hook I'm using.
The perfect solution would be a hook wich:
- Is fired after the post meta are saved
- Allow me to check the post_status of the post before it was saved.
I tried the hook post updated
but I got the same result: the custom field does not show up.
Which hook could I use? Or I should try a totally different approach?
I have a custom post type immobile
with some custom fields created with Toolset Types.
I need to send and email with the title and a custom field when a new immobile
is saved as a draft or it's published.
I managed to do almost everthing. My code is like this:
add_action( 'auto-draft_to_draft', 'my_send_email', 99 );
add_action( 'auto-draft_to_publish', 'my_send_email', 99 );
add_action( 'draft_to_publish', 'my_send_email', 99 );
function my_send_email( $post ) {
if ( $post->post_type == 'immobile' ) {
wp_mail(
'[email protected]',
'New immobile saved as ' . $post->post_status,
'Title: ' . $post->post_title . '. Ref: ' . get_post_meta( $post->ID, 'wpcf-reference', true ),
array('Content-Type: text/html; charset=UTF-8')
);
}
}
Unfortunately my custom field is not showing in the email, probably because It get saved after the hook I'm using.
The perfect solution would be a hook wich:
- Is fired after the post meta are saved
- Allow me to check the post_status of the post before it was saved.
I tried the hook post updated
but I got the same result: the custom field does not show up.
Which hook could I use? Or I should try a totally different approach?
Share Improve this question edited Jun 11, 2020 at 16:46 Pacicio asked Jun 11, 2020 at 15:34 PacicioPacicio 213 bronze badges 3- 1 Not familiar with toolset , it might be because it saves the value after post is published . Does it have separate action like acf/save_post ? – Anoop D Commented Jun 11, 2020 at 16:01
- Welcome to WordPress Development. I hope you find the answer(s) you are looking for. Our site is different from most - if you have not done so yet, consider checking out the tour and help center to find out how things work. – Matthew Brown aka Lord Matt Commented Jun 11, 2020 at 16:01
- @AnoopD unfortunately their documentation says "Types does not have a proper hooks API". However, this applies to all custom fields, not only the ones created with Toolset – Pacicio Commented Jun 11, 2020 at 16:38
1 Answer
Reset to default 0I think you need to handle post request first. You have to do this because gutenberg calls the hook twice.
function transition_post_status_handler( $new_status, $old_status, $post ) {
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
send_mail_on_publish( $new_status, $old_status, $post );
set_transient( 'post_status_updater_flag', 'done', 10 );
} else {
if ( false === get_transient( 'post_status_updater_flag' ) )
send_mail_on_publish( $new_status, $old_status, $post );
}
}
add_action( 'transition_post_status', 'transition_post_status_handler', 10, 3 );
Then you can call the function to send the email. At this point I think you have the data of the meta available.
function send_mail_on_publish( $new_status, $old_status, $post ) {
if ( 'publish' == $new_status && 'immobile' == get_post_type($post) ) {
// Check if not an autosave
if ( wp_is_post_autosave( $post->ID ) )
return;
// Check if not a revision
if ( wp_is_post_revision( $post->ID ) )
return;
// Send email
}
}
本文标签: hooksSend email with custom fields after new draft is saved or new post published
版权声明:本文标题:hooks - Send email with custom fields after new draft is saved or new post published 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742353524a2458961.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论