发布新帖子时通知管理员或作者

时间:2013-10-03 作者:Kevin S

我正在使用一个带有“publish\\u post”钩子的自定义函数,以便在发布帖子时通知作者,但我现在面临的问题是,通知被发送了两次,以及在更新帖子时。下面是我的函数的外观。

function authorNotification($post_id) {
   $post = get_post($post_id);
   $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(\'publish_post\', \'authorNotification\');
我错过什么了吗?

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

你需要写你的钩子transition_post_status 措施:

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 );

结束

相关推荐

如何使用POSTS_WHERE/POSTS_JOIN创建自己的嵌套META_QUERY?

我的一些帖子(不是全部)有一个价格作为元键/值。今天我使用pre_get_posts 操作,以便我的用户可以搜索介于某个值之间的价格。这是我今天使用的代码,它正在运行。add_action(\'pre_get_posts\', \'my_search_price\'); function my_search_price( $query ) { if ($query->get(\'maxprice\') != \"\" && $query->get(\'m