1

I'm just starting out with making plugins and I'm trying to make a simple one that will mail a notification to subscribers whenever I publish a new post.

My code so far:

add_action( 'publish_post', 'vb_esa_update_email' ); 
function vb_esa_update_email( $post_id ) { 

    //verify post is not a revision 
    if ( !wp_is_post_revision( $post_id ) ) { 

        //gets subscirbers to send email to
        // WP_User_Query arguments
        $args = array (
            'role'           => 'Subscriber',
        );

        // The User Query
        $user_query = new WP_User_Query( $args );

        $post_title = get_the_title( $post_id ); 
        $post_url = get_permalink( $post_id ); 
        $subject = 'A post has been updated'; 
        $message = "A post has been updated on your website:\n\n";
        $message .= "<a href='". $post_url. "'>" .$post_title. "</a>\n\n"; 
        //send email to 
        foreach($args as $email_address)
        {
            wp_mail($email_address, $subject, $message );
        }
    }
} 

How do I go about filling up an array with the list of subscribers to send the notifications to?

3 Answers 3

1

You were on the right track when you built your WP_User_Query, but you are not using the result of this query. See this:

 //verify post is not a revision 
if ( !wp_is_post_revision( $post_id ) ) { 

        //gets subscirbers to send email to
        // WP_User_Query arguments
        $args = array (
            'role'           => 'Subscriber',
        );


        // The User Query
        $user_query = new WP_User_Query( $args );

        // get email addresses from user objects
         $email_addresses = array();
        foreach ( $user_query->results as $user ) {
            $email_addresses[] = $user->user_email;
        }

        // build message
        $post_title = get_the_title( $post_id ); 
        $post_url = get_permalink( $post_id ); 
        $subject = 'A post has been updated'; 
        $message = "A post has been updated on your website:\n\n";
        $message .= "<a href='". $post_url. "'>" .$post_title. "</a>\n\n"; 

        //send email to all emails
        wp_mail($email_addresses, $subject, $message );

}
  • we loop all users and build an array with each email address
  • we use this array directly as a parameter of wp_mail() (it supports arrays)

Note that you would probably need to use a third-party service to send many mails at once, or you could have problem with your hosting provider. Have a look at Mandrill. They have a WordPress plugin that works well with the wp_mail() function.

2
  • thanks for that, i just didn't how to fill the array with the users email's, i'm still relatively new to php and WP development, but I'm trying to slog through it. I'm not too concerned yet about sending many emails at once, as its just for practice atm, but i'll look into Mandrill!
    – gidea
    Commented Feb 13, 2015 at 9:57
  • you are welcome ! Have fun with WordPress :) Commented Feb 13, 2015 at 10:11
0

You're almost there but you were not actually picking up any of email addresses from your User Query

This code should do what you want, however if my understanding of your question is correct you want to send an individual email to each Subscriber, which is not efficient.

It would be better to add all email addresses to an array and send the emails in one go (the wp_mail function will seperate them correctly for you). Better yet, add the addresses to the BCC field, then your Subscribers will not be able to see the addresses of all the other subscribers.

add_action('publish_post', 'vb_esa_update_email'); 
function vb_esa_update_email($post_id){ 

    /** Ensure that the post is not a revision */
    if(wp_is_post_revision($post_id)) :
        return;

    /** Query all users who have the role 'Subscriber' */
    $args = array (
        'role'           => 'Subscriber',
    );
    $user_query = new WP_User_Query($args);

    /** Check to see if there are any matching users */
    if(!empty($user_query->results)) : 

        /** Set up the email subject and message */
        $post_title = get_the_title( $post_id ); 
        $post_url = get_permalink( $post_id ); 
        $subject = 'A post has been updated'; 
        $message = "A post has been updated on your website:\n\n";
        $message.= "<a href='". $post_url. "'>" .$post_title. "</a>\n\n"; 

        /** Send an individual message to each user */
        foreach($user_query->results as $user) :

            wp_mail($user->data->user_email, $subject, $message);

        endforeach;

    endif;

}
1
  • So this sends one email each time it runs through the for loop right. I'ts not quite what i had in mind as i want to send just a general newsletter to subscribers, but looking at thomascharbits code above it looks like that send them in one batch. The BCC is something that i'd like to look into! I've seen a tutorial on how to bcc mass emails so I'll try add it to my own. Thanks for the help!
    – gidea
    Commented Feb 13, 2015 at 10:06
0

Here is an answer to a similar question that may help. You should adjust the code in above answer to select users with role subscriber.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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