默认情况下,get_users()
仅返回当前博客的用户。通过设置参数,可以更清楚地说明这一点blog_id
:
$users = get_users( array ( \'blog_id\' => $GLOBALS[\'blog_id\'] ) );
但这是不必要的。
现在,将Execute function when post is published 和Multiple recipients for wp_mail()
, 我们可以使用这样的方法:
获取电子邮件地址
function get_notify_emails( $post, $blog_id = NULL )
{
if ( NULL === $blog_id )
$blog_id = $GLOBALS[ \'blog_id\' ];
$users = get_users( array ( \'blog_id\' => $blog_id ) );
if ( empty ( $users ) ) // something went wrong
return array ();
$recipients = array ();
$from = \'\';
foreach ( $users as $user )
{
// set sender
if ( isset ( $user->caps[ \'administrator\' ] )
&& \'1\' === $user->caps[ \'administrator\' ]
&& \'\' === $from
)
{
$from = "{$user->data->display_name} <{$user->data->user_email}>";
continue;
}
// exclude super admins
if ( is_super_admin( $user->ID ) )
continue;
// exclude post author
if ( (int) $post->post_author === (int) $user->ID )
continue;
// add the other users as recipients
$recipients[] = "{$user->data->display_name} <{$user->data->user_email}>";
}
return ( array ( $from, $recipients ) );
}
发送电子邮件
add_action( \'transition_post_status\', \'notify_blog_users_on_new_posts\', 10, 3 );
function notify_blog_users_on_new_posts( $new_status, $old_status, $post )
{
if ( \'publish\' !== $new_status or \'publish\' === $old_status )
return;
$post_types = get_post_types( array ( \'publicly_queryable\' => TRUE ) );
if ( ! in_array( $post->post_type, $post_types ) )
return;
list ( $from, $to ) = get_notify_emails( $post );
$subject = \'New post on \' . get_bloginfo( \'name\' ) . \': \' . get_the_title( $post );
$headers = array ( "from:$from" );
$message = \'Hello,
there is a new post on our blog:
\' . get_the_title( $post ) . \'
<\' . get_permalink( $post->ID ) . \'>
Enjoy!\';
wp_mail( $to, $subject, $message, $headers );
}
这只是一个草稿,需要一些测试,我可能会将代码分为多个专用类。但它应该给出如何进行的想法。