One thing you can do is to filter $email_to
and return empty string if the recipient is logged in. This way wp_mail()
will fail to send the message and return false. Add the following to theme functions.php
or to bp-custom.php
file:
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: A possible solution for your case with the plugin you use is to get all the users who have that e-mail and check if any is online by passing that list to bp_has_members()
function:
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;
}