如何使用wp_mail向订阅者发送邮件

时间:2015-02-12 作者:gidea

我刚刚开始制作插件,我正在尝试制作一个简单的插件,每当我发布新帖子时,它都会向订阅者发送通知。

迄今为止我的代码:

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 );
        }
    }
} 
如何用要向其发送通知的订阅者列表填充数组?

3 个回复
SO网友:thomascharbit

构建WP\\u User\\u查询时,您的思路是正确的,但您没有使用此查询的结果。请参见:

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

}
我们循环所有用户并使用每个电子邮件地址构建一个数组,我们直接将此数组用作wp_mail() (它支持阵列)请注意,您可能需要使用第三方服务一次发送多封邮件,否则您可能会与托管提供商发生问题。看看Mandrill. 他们有一个WordPress插件,可以很好地与wp_mail() 作用

SO网友:David Gard

你就快到了,但实际上你没有从你的User Query

此代码应该满足您的要求,但是,如果我对您的问题理解正确,您希望向每个订阅者发送一封单独的电子邮件,这是没有效率的。

最好将所有电子邮件地址添加到一个数组中,并一次性发送电子邮件(即wp_mail 函数将为您正确分隔它们)。更好的是,将地址添加到密件抄送字段,这样您的订阅者将无法看到所有其他订阅者的地址。

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;

}

SO网友:Qaisar Feroz

这是一个answer to a similar question 这可能会有所帮助。您应该调整上述答案中的代码,以选择具有角色的用户subscriber.

结束

相关推荐

Multisite and plugins options

我需要更多关于get_site_option(), update_site_option() 和add_site_option().在codex中,他们说它与单次安装相同,只是在多站点中,它返回网络范围的选项。好吧,但我对这一点感到困惑:这是否意味着一旦设置了网络范围选项,所有站点的选项都是相同的?还是每个站点都会覆盖它?我的意思是如果我这样做: update_site_option(\'option_group\', \'default_options());// default_options() r