您可以使用transition_post_status
函数,然后获取用户并向所有用户发送电子邮件。
这是一个示例代码,它没有经过测试。但这会让你从这个开始。
function wcs_send_mail_on_publish_category_posts( $new_status, $old_status, $post ) {
global $post;
if ( \'publish\' !== $new_status or \'publish\' === $old_status ) return;
if ( in_category( array( \'12\', \'34\' ) ) ) {
$subscribers = get_users( array ( \'role\' => \'subscriber\' ) );
$emails = array ();
foreach ( $subscribers as $subscriber )
$emails[] = $subscriber->user_email;
$body = sprintf( \'Hey there is a new entry! See <%s>\', get_permalink( $post ) );
wp_mail( $emails, \'New entry!\', $body );
}
}
add_action( \'transition_post_status\', \'wcs_send_mail_on_publish_category_posts\', 10, 3 );