This question already has an answer here: How to auto send email when publishing a custom post type? (1 answer) Closed 3 years ago.
admin 管理员组文章数量: 1184232
The code below works perfectly
When I create a new post, an email is sent out to the email included in the function.
I would like to know how I can send an email when anything is published on the website, like custom post types, pages and regular posts
function notifyauthor($post_id) {
$post = get_post($post_id);
$author = get_userdata($post->post_author);
$subject = "Post Published: ".$post->post_title."";
$message = "
Hi ".$author->display_name.",
Your post, \"".$post->post_title."\" has just been published.
View post: ".get_permalink( $post_id )."
Thanks"
;
wp_mail("[email protected]", $subject, $message);
}
add_action('publish_post', 'notifyauthor');
This question already has an answer here:
How to auto send email when publishing a custom post type?
(1 answer)
Closed 3 years ago.
The code below works perfectly
When I create a new post, an email is sent out to the email included in the function.
I would like to know how I can send an email when anything is published on the website, like custom post types, pages and regular posts
function notifyauthor($post_id) {
$post = get_post($post_id);
$author = get_userdata($post->post_author);
$subject = "Post Published: ".$post->post_title."";
$message = "
Hi ".$author->display_name.",
Your post, \"".$post->post_title."\" has just been published.
View post: ".get_permalink( $post_id )."
Thanks"
;
wp_mail("[email protected]", $subject, $message);
}
add_action('publish_post', 'notifyauthor');
Share
Improve this question
edited Apr 9, 2021 at 13:01
Neha Patel
asked Apr 9, 2021 at 3:22
Neha PatelNeha Patel
538 bronze badges
0
1 Answer
Reset to default 2The hook that you're currently using is publish_<post type> which means the <post type> part is dynamic and the value is a post type name/slug.
So for other post types like page and (a custom post type named) my_cpt, you would just need to change the "post" (or the <post type> part) in the hook name to page, my_cpt or whatever is the post type name/slug like so:
add_action( 'publish_post', 'notifyauthor' ); // for the default 'post' post type
add_action( 'publish_page', 'notifyauthor' ); // for the default 'page' post type
add_action( 'publish_my_cpt', 'notifyauthor' ); // for a CPT with the name/slug my_cpt
Or if you want your action (notifyauthor()) to be called when any posts (regular Posts and Pages, and custom post types) is published, then you would want to use the transition_post_status() hook instead of the publish_<post type> hook.
Here's an example based on this on the WordPress Developer Resources site:
function wpdocs_run_on_publish_only( $new_status, $old_status, $post ) {
// Yes, I said "any posts", but you might better off specify a list of post
// types instead. Or you could do the check in your notifyauthor() function
// instead.
$post_types = array( 'post', 'page', 'my_cpt', 'foo_bar', 'etc' );
if ( ( 'publish' === $new_status && 'publish' !== $old_status ) &&
in_array( $post->post_type, $post_types )
) {
notifyauthor( $post->ID );
}
}
add_action( 'transition_post_status', 'wpdocs_run_on_publish_only', 10, 3 );
PS: If you use the above code/hook, then you should remove the add_action('publish_post', 'notifyauthor'); from your current code.
本文标签:
版权声明:本文标题:hooks - I would like to send a notification email (Asana) whenever something is published (posts, pages, custom post types) 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1741625154a2306332.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论