0

Context: Creating a MultiSite in which each Sub-Site will have it's own set of users that they belong to. These users are only able to view their own site.

I was wondering what the easiest way to mass-email the users of each Sub-Site would be? For instance, when a new News post is put in the blog, it will send a notification to the members of that Sub-Site, but noone else. I have found Plug-ins that would mass-email the whole network, but it seems to be hard to find one that can separate from the network and the Sub-Sites. It would be the admins/owners of the Sub-Sites that will be sending the email, not the Super-admin.

0

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.

  • In my tests, it only sends to users of the current blog, not to those not a part of that site. -- That stands to reason. To get users of another site on the network, you'd need to use switch_to_blog(), I expect. – Pat J Sep 30 '13 at 23:10
0

By default, get_users() returns the users for the current blog only. You can make that more clear by setting the argument blog_id:

$users = get_users( array ( 'blog_id' => $GLOBALS['blog_id'] ) );

But that shouldn’t be needed.

Now, combining the codes from Execute function when post is published and Multiple recipients for wp_mail(), we can use something like this:

Get the email addresses

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 ) );
}

Send the emails

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 );
}

This is just a draft, it needs some tests, and I would probably separate the code into multiple, specialized classes. But it should give an idea how to proceed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.