当自定义帖子状态更改为已发布时,向帖子作者发送电子邮件

时间:2018-11-07 作者:Alt C

我参考了这个答案:

https://wordpress.stackexchange.com/a/100657/145078

它发送给所有用户。

我只想在发布作者的自定义帖子类型时通知作者。

请帮忙谢谢

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

这应该有效—确保更改my_custom_type 至正确的CPT段塞:

add_action( \'transition_post_status\', \'notify_author_on_publish\', 10, 3 );
function notify_author_on_publish( $new_status, $old_status, $post ) {
    if ( \'publish\' !== $new_status ||
        $new_status === $old_status ||
        \'my_custom_type\' !== get_post_type( $post ) ) {
        return;
    }

    // Get the post author data.
    if ( ! $user = get_userdata( $post->post_author ) ) {
        return;
    }

    // Compose the email message.
    $body = sprintf( \'Hey %s, your awesome post has been published!
    See <%s>\',
        esc_html( $user->display_name ),
        get_permalink( $post )
    );

    // Now send to the post author.
    wp_mail( $user->user_email, \'Your post published!\', $body );
}

结束