首先,@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() 如果帖子的状态从任何内容更改为发布,请通知所有用户。