仅向编辑发送电子邮件通知

时间:2019-03-25 作者:sofarocker

当一篇新文章发表后,我想向我博客的所有编辑发送一封电子邮件。我找到了很多插件,但我想在没有插件的情况下解决这个问题。此外,我还找到了一个发送电子邮件的教程,但这只适用于博客管理员,不适用于编辑(Front-End Editor email notification issue). 如果你能给我小费或建议,我会很高兴的。非常感谢。

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

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

使用get_users() 作用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\' );  
你可以阅读更多here, 关于可选headers 可以添加以下字段的参数from, cc, content-type

相关推荐

正在尝试获取wp-includes/capabilities.php中非对象的属性

在调试中,我每分钟都会收到以下通知序列。日志:[23-Oct-2012 13:27:33 UTC] PHP Notice: Trying to get property of non-object in mysite/wp-includes/capabilities.php on line 1022 [23-Oct-2012 13:27:33 UTC] PHP Notice: Trying to get property of non-object in mysite/wp-includes/