伙伴新闻仅在用户未登录时发送电子邮件通知

时间:2012-11-26 作者:Arystark

当一个成员向另一个成员发送消息时,收件人会收到buddypress中的消息和电子邮件。

我想改变这一点,因为如果你收到很多邮件,即使你登录了,你的邮箱中也有太多的邮件:我只想在没有登录BuddyPress的情况下接收邮件。

我找到了添加代码的地方,但我不知道如何挂钩(add\\u action或add\\u filter)。文件是:wp content/plugins/buddypress/bp messages/bp messages notifications。php

修改在文件末尾,只需在发送电子邮件之前添加if测试:

if (!is_user_online($recipient->user_id)) {
     wp_mail( $email_to, $email_subject, $email_content );
}
如何在不更改buddypress的核心文件的情况下做到这一点?

1 个回复
SO网友:Ahmad M

你可以做的一件事就是过滤$email_to 如果收件人已登录,则返回空字符串。这边wp_mail() 将无法发送消息并返回false。将以下内容添加到主题functions.php 或至bp-custom.php 文件:

add_filter(\'messages_notification_new_message_to\', \'disable_loggedin_email_notification\');
function disable_loggedin_email_notification($email_to) {
    $user = get_user_by(\'email\',$email_to);
    if (bp_has_members("type=online&include=$user->ID")) {
        $email_to = \'\';
    }
    return $email_to;
}
EDIT: 对于您使用的插件,一个可能的解决方案是让所有拥有该电子邮件的用户通过将该列表传递给bp_has_members() 功能:

add_filter(\'messages_notification_new_message_to\', \'disable_loggedin_email_notification\');

function disable_loggedin_email_notification($email_to) {
    $users = get_users(array(
        \'search\' => $email_to
    ));
    $ids = array();
    foreach ($users as $user) {
        $ids[] = $user->ID;
    }
    $ids = implode(\',\', $ids);
    if (bp_has_members("type=online&include=$ids")) {
        $email_to = \'\';
    }
    return $email_to;
}

结束