当作者和贡献者发帖时,如何发送发帖通知?

时间:2014-05-14 作者:facebook-100001191095620

我在一个多用户博客上使用下面的代码,以便在发布新帖子时向用户发送电子邮件通知。但问题是,它只在管理员和编辑发布时发送通知。我想在管理员、编辑、作者和贡献者发布任何东西时发送通知。因此,对于这项工作,我应该更改以下代码,请紧急帮助:

function new_mail_from($old) {
 return \'[email protected]\';
}
function new_mail_from_name($old) {
 return \'Postmaster - BIZCATALYST360°\';
}

/** Notification Upon post Pubished */
function authorNotification( $new_status, $old_status, $post ) {
    if ( $new_status == \'publish\' && $old_status != \'publish\' ) {
        $author = get_userdata($post->post_author);
        $message = "
            Hi ".$author->display_name.",
            New post, ".$post->post_title." has just been published at ".get_permalink(      $post->ID ).". ";
        wp_mail($author->user_email, "$post->post_title", $message);
    }
}
add_action(\'transition_post_status\', \'authorNotification\', 10, 3 );

4 个回复
SO网友:cybmeta

我只找到_transition_post_status 它被标记为private,这意味着它不适合插件和主题开发人员使用。我找不到transition_post_status 操作,您正在使用的操作。尝试publish_post 而不是动作挂钩。请参见此示例,对直接取自de Codex的示例进行了一些修改:

<?php

// SEND EMAIL ONCE POST IS PUBLISHED

function notify_new_post($post_id) {
    if( ( $_POST[\'post_status\'] == \'publish\' ) && ( $_POST[\'original_post_status\'] != \'publish\' ) ) {
         $post = get_post($post_id);
         $email_subject = "A new post has been published.";

         ob_start(); ?>

        <html>
            <head>
                <title>New post at <?php bloginfo( \'name\' ) ?></title>
            </head>
            <body>
                <p>
                Hi!!!,
                </p>
                <p>
                There is a new post: <a href="<?php echo get_permalink($post->ID) ?>"><?php the_title_attribute() ?></a>.
                </p>
            </body>
        </html>

        <?php

        $message = ob_get_contents();

        ob_end_clean();

        //Get the email field of all users
        $users = get_users( array( \'fields\' => array( \'user_email\' ) ) );            
        foreach( $users as $user ) {
             wp_mail( $user->user_email, $email_subject, $message );
        }
    }
}

add_action( \'publish_post\', \'notify_new_post\' ); ?>
编辑我现在很熟悉transition_post_status action. 你遇到的问题是发送电子邮件的逻辑,而不是行为。下一个示例与上述代码段类似,但使用transition_post_status 措施:

add_action(\'transition_post_status\', \'notify_new_post\', 10, 3 );
function notify_new_post( $new_status, $old_status, $post ) {
    if ( $new_status == \'publish\' && $old_status != \'publish\' ) {

       $email_subject = "A new post has been published.";

       ob_start(); ?>

       <html>
         <head>
            <title>New post at <?php bloginfo( \'name\' ) ?></title>
         </head>
         <body>
            <p>
            Hi!!!,
            </p>
            <p>
            There is a new post: <a href="<?php echo get_permalink($post->ID) ?>"><?php the_title_attribute() ?></a>.
            </p>
         </body>
       </html>

       <?php

       $message = ob_get_contents();

       ob_end_clean();

        //Get the email field of all users
        $users = get_users( array( \'fields\' => array( \'user_email\' ) ) );            
        foreach( $users as $user ) {
             wp_mail( $user->user_email, $email_subject, $message );
        }
    }
}

SO网友:denis.stoyanov

试试这个。我已经对它进行了测试,虽然您的问题不是很清楚(代码片段与描述)。

function wpkse_144405_on_post_publish( $ID, $post ) {
    $users = get_users( array( \'fields\' => array( \'user_email\' ) ) );   
    $email_subject = \'New post has been published\';
    $message .= \'New post has been published: \' . $post->post_title 
             .  \' You can preview it here: \' . get_permalink( $ID );

    foreach( $users as $user ) {
        wp_mail( $user->user_email, $email_subject, $message );
    }
}
add_action(  \'publish_post\',  \'wpkse_144405_on_post_publish\', 10, 2 );
参考号:wp_mail(), Post Status Transitions

SO网友:Abhik

首先,@cybmeta是正确的。您的代码无法将您的预期通知发送给所有注册用户。它目前只向帖子作者发送通知。

无论如何,您可以尝试以下方法:

function send_notification_of_new_post( $post ) {

    $users = get_users( array( \'fields\' => array( \'user_email\', \'display_name\' ) ) );       
    foreach( $users as $user ) {
        wp_mail( $user->user_email, $post->post_title, \'Hi<br>\' .$user->display_name.\',<br>New post, \' . $post->post_title . \' has just been published at \' . get_permalink( $post->ID ) . \'.\' );
    }
}
add_action( \'pending_to_publish\', \'send_notification_of_new_post\', 10, 1 );
add_action( \'draft_to_publish\', \'send_notification_of_new_post\', 10, 1 );
add_action( \'auto-draft_to_publish\', \'send_notification_of_new_post\', 10, 1 );
add_action( \'future_to_publish\', \'send_notification_of_new_post\', 10, 1 );
add_action( \'private_to_publish\', \'send_notification_of_new_post\', 10, 1 );
这使用Post Status Transitions 确定状态更改和使用wp_mail() 如果帖子的状态从任何内容更改为发布,请通知所有用户。

SO网友:Sandy

试试这个对我有用

function authorNotification( $new_status, $old_status, $post ) {
    if ( $new_status == \'publish\' && $old_status != \'publish\' ) {
        $author = get_userdata($post->post_author);
        $message = "
            Hi ".$author->display_name.",
            New post, ".$post->post_title." has just been published at ".get_permalink( $post->ID ).".
        ";
        wp_mail($author->user_email, "New Post Published", $message);
    }
}
add_action(\'transition_post_status\', \'authorNotification\', 10, 3 );

结束

相关推荐

WP Cron emails not working

运行wp 3.5.1我有两个插件可以部分工作。他们做的每件事都很好。如果我点击发送电子邮件,他们就会发送电子邮件。然而scheduled 未发送电子邮件。插件包括:Wordpress file monitor plus 2.2版WP db manager 2.63版TROUBLESHOOTING我试着将时间表改为每小时、每天、每2天运行一次。但是他们没有一个人发送电子邮件,但其余的都完成了。WP db manager在正确的时间生成备份。Wordpress file monitor plus会在需要时检查