admin管理员组文章数量:1429570
I would like to send an email to all editors of my blog when a new article has been published. I found many plugins but I want to solve this problem without plugins. In addition, I found a tutorial to send an email, but this is only for administrators of the blog but not for editors (Front-End Editor email notification issue). I would be happy if you could give me a tip or advice. Thank you.
<?php
function notifyauthor( $post_id ) {
$post = get_post( $post_id );
$author = get_userdata( $post->post_author ); // How can I specify the role of the editors here?
$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( $role->user_email, $subject, $message );
}
add_action( 'publish_post', 'notifyauthor' );
I would like to send an email to all editors of my blog when a new article has been published. I found many plugins but I want to solve this problem without plugins. In addition, I found a tutorial to send an email, but this is only for administrators of the blog but not for editors (Front-End Editor email notification issue). I would be happy if you could give me a tip or advice. Thank you.
<?php
function notifyauthor( $post_id ) {
$post = get_post( $post_id );
$author = get_userdata( $post->post_author ); // How can I specify the role of the editors here?
$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( $role->user_email, $subject, $message );
}
add_action( 'publish_post', 'notifyauthor' );
Share
Improve this question
edited Mar 25, 2019 at 14:32
Max Yudin
6,3882 gold badges26 silver badges36 bronze badges
asked Mar 25, 2019 at 11:03
sofarockersofarocker
31 bronze badge
2
- 1 So you want to find a list of users by role so that you can loop through them and email them all? wordpress.stackexchange/q/219686/3276 – Rup Commented Mar 25, 2019 at 12:37
- Thanks for your answer. Yes exactly. All editors should receive an email when an amount has been published. Admins are excluded. Do you know how I can build the code? – sofarocker Commented Mar 25, 2019 at 12:44
1 Answer
Reset to default 1Use get_users()
function. Reference
function notify_editors( $post_id ) {
$post = get_post( $post_id );
// Get all editors
$editors = get_users( [ 'role__in' => [ 'editor'] ] );
foreach ( $editors as $editor ) {
// Setup email
$subject = "Post Published: " . $post->post_title;
$message = ' Hi ' . $editor->display_name . ',
Your post, "' . $post->post_title . '" has just been published.
View post: ' . get_permalink( $post_id ) . 'Thanks';
// and send...
wp_mail( $editor->user_email, $subject, $message );
}
}
add_action( 'publish_post', 'notify_editors' );
You can read more here, about optional headers
parameter that can add fields like from
, cc
, content-type
etc.
本文标签: user rolesEmail notification for editors only
版权声明:本文标题:user roles - Email notification for editors only 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745543796a2662613.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论