Emailing only the users of a MultiSites Sub-Sites

I’m actually in the process of creating a plugin that does this, and it doesn’t seem as if there are any special considerations needed for multisite sub-blogs. When the function checks for users of the blog, it only grabs those from the current blog (in my testing).

Here’s some of my code to give you an idea of how I’m doing this in my plugin:

add_action("publish_post", "post_notification");

function post_notification($post_id) {

if (in_category( 'cat-slug' ) ) {


$post = get_post($post_id);
$author = get_userdata($post->post_author);
$email_subject = "Email Subject Here";


ob_start();

include("email_header.php");

?>

//email content goes here


<?php

include("email_footer.php");


$message = ob_get_contents();

ob_end_clean();

$users = get_users();
$emails = array();

foreach ($users as $user) {


wp_mail ($emails[] = $user->user_email, $email_subject, $message);
}
}

}

The most pertinent area is that begins with $users, where I grab all the users, then send an email to each one by putting each email address in the $to field of wp_mail. In my tests, it only sends to users of the current blog, not to those not a part of that site.

Leave a Comment