admin管理员组

文章数量:1022949

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?

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?

Share Improve this question edited Apr 12, 2019 at 16:31 butlerblog 5,1313 gold badges28 silver badges44 bronze badges asked Feb 12, 2015 at 12:55 gideagidea 111 silver badge4 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 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.

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;

}

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.

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?

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?

Share Improve this question edited Apr 12, 2019 at 16:31 butlerblog 5,1313 gold badges28 silver badges44 bronze badges asked Feb 12, 2015 at 12:55 gideagidea 111 silver badge4 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 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.

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;

}

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.

本文标签: pluginsHow to send mail to subscribers using wpmail