bbPress have two types of notifications: new message in topic or new topic in forum. Functions that sends messages in bbpress/includes/common/functions.php. You can’t redeclare they. You need to remove default functions and add your custom one. Here is my working code for armyofselenagomez.com wich sends emails via external SMTP server (like Mandrill. but I use Yandex, like Gmail but in Russia). Be careful with priorities and numbers of arguments to pass.
Logis of process:
- User leave a message or create a topic.
- Create single wp-crone task which create messages and send it.
Positive sides of this solution: Emails sends per user (not 1 email per 100000 users like in bbPress by default). Site work faster because emails send via cron, not immediately. I dont store emails or messages to send in DB, just topic ID. Emails sends via SMTP server (not just PHP Mailer or wp_mail()).
<?php
function selena_network_bbp_init () {
/*
* Оповещание о новых ответах в темах на форуме
* 1. Удаляем дефолтную оповещалку.
* 2. Добавляем свою оповещалку, которая создает крон-задачу.
* 3. Экшн фильтр задачи, который создает письма и отправляет их.
*
* Функции находятся в файле
* bbpress/includes/common/functions.php
*/
remove_action ('bbp_new_reply', 'bbp_notify_subscribers', 11);
add_action ('bbp_new_reply', 'selena_network_bbp_notify_subscribers', 11, 5);
add_action ('selena_network_bbp_notify_subscribers_cron', 'selena_network_bbp_notify_subscribers_callback', 10, 5);
/*
* Оповещения о создании новых тем на форуме
* 1. Удаляем дефолтную оповещалку.
* 2. Добавляем свою оповещалку, которая создает крон-задачу.
* 3. Экшн для задачи, который будет запускать крон (создает письма и отправляет их).
*
* Функции находятся в файле
* bbpress/includes/common/functions.php
*/
remove_action ('bbp_new_topic', 'bbp_notify_forum_subscribers', 11);
add_action ('bbp_new_topic', 'selena_network_bbp_notify_forum_subscribers', 11, 4);
add_action ('selena_network_bbp_notify_forum_subscribers_cron', 'selena_network_bbp_notify_forum_subscribers_callback', 10, 4);
}
add_action ('bbp_init', 'selena_network_bbp_init', 99);
<?php
function selena_network_bbp_notify_subscribers (
$reply_id = 0,
$topic_id = 0,
$forum_id = 0,
$anonymous_data = false,
$reply_author = 0
) {
wp_schedule_single_event (
time (),
'selena_network_bbp_notify_subscribers_cron',
array (
$reply_id,
$topic_id,
$forum_id,
$anonymous_data,
$reply_author
)
);
return true;
}
function selena_network_bbp_notify_subscribers_callback (
$reply_id,
$topic_id,
$forum_id,
$anonymous_data,
$reply_author
) {
// Bail if subscriptions are turned off
if ( !bbp_is_subscriptions_active() ) {
return false;
}
$reply_id = bbp_get_reply_id( $reply_id );
$topic_id = bbp_get_topic_id( $topic_id );
$forum_id = bbp_get_forum_id( $forum_id );
// Bail if topic is not published
if ( !bbp_is_topic_published( $topic_id ) ) {
return false;
}
// Bail if reply is not published
if ( !bbp_is_reply_published( $reply_id ) ) {
return false;
}
// User Subscribers
$user_ids = bbp_get_topic_subscribers( $topic_id, true );
if ( empty( $user_ids ) ) {
return false;
}
// Poster name
$reply_author_name = bbp_get_reply_author_display_name( $reply_id );
// Remove filters from reply content and topic title to prevent content
// from being encoded with HTML entities, wrapped in paragraph tags, etc...
remove_all_filters( 'bbp_get_reply_content' );
remove_all_filters( 'bbp_get_topic_title' );
// Strip tags from text and setup mail data
$topic_title = strip_tags( bbp_get_topic_title( $topic_id ) );
$reply_url = bbp_get_reply_url( $reply_id );
$subject = sprintf(
_x (
'%1$s wrote new comment in “%2$s”',
'%1$s = Name of user who create comment
%2$s = Topic title in which user leave a comment',
'selena_network'),
$reply_author_name,
$topic_title
);
if (empty ($subject)) {
return;
}
// Loop through users
foreach ( (array) $user_ids as $user_id ) {
// Don't send notifications to the person who made the post
if ( !empty( $reply_author ) && (int) $user_id === (int) $reply_author ) {
continue;
}
// For plugins to filter messages per reply/topic/user
$message = sprintf (
_x (
'%1$s wrote new comment in conversation “%2$s” %3$s.
+
Notification settings → %4$s.
Send email at %5$s or respond on that letter if you have a questions.',
'%1$s = Reply author name
%2$s = Topic title
%3$s = Reply URL to this topic
%4$s = Manage subscribe settings link
%5$s = Admin email',
'selena_network'
),
$reply_author_name,
$topic_title,
$reply_url,
bbp_get_subscriptions_permalink ($user_id),
get_option ('admin_email')
);
if ( empty( $message ) ) {
continue;
}
wp_mail (
get_userdata( $user_id )->user_email,
$subject,
$message
);
}
return true;
}
/* Новая тема на форуме */
function selena_network_bbp_notify_forum_subscribers (
$topic_id = 0,
$forum_id = 0,
$anonymous_data = false,
$topic_author = 0
) {
wp_schedule_single_event (
time (),
'selena_network_bbp_notify_forum_subscribers_cron',
array (
$topic_id,
$forum_id,
$anonymous_data,
$topic_author
)
);
return true;
}
function selena_network_bbp_notify_forum_subscribers_callback (
$topic_id,
$forum_id,
$anonymous_data,
$topic_author
) {
// Bail if subscriptions are turned off
if ( !bbp_is_subscriptions_active() ) {
return false;
}
$topic_id = bbp_get_topic_id( $topic_id );
$forum_id = bbp_get_forum_id( $forum_id );
// Get topic subscribers and bail if empty
$user_ids = bbp_get_forum_subscribers( $forum_id, true );
if ( empty( $user_ids ) ) {
return false;
}
// Bail if topic is not published
if ( ! bbp_is_topic_published( $topic_id ) ) {
return false;
}
// Poster name
$topic_author_name = bbp_get_topic_author_display_name( $topic_id );
// Remove filters from reply content and topic title to prevent content
// from being encoded with HTML entities, wrapped in paragraph tags, etc...
remove_all_filters( 'bbp_get_topic_content' );
remove_all_filters( 'bbp_get_topic_title' );
// Strip tags from text and setup mail data
$topic_title = strip_tags( bbp_get_topic_title( $topic_id ) );
$forum_title = strip_tags( bbp_get_forum_title( $forum_id ) );
$topic_url = get_permalink( $topic_id );
// For plugins to filter titles per reply/topic/user
//$subject = apply_filters( 'bbp_forum_subscription_mail_title', '[' . $blog_name . '] ' . $topic_title, $topic_id, $forum_id, $user_id );
$subject = sprintf (
_x (
'%1$s created new conversation “%2$s”',
'%1$s = Topic author name who create new new topic
%2$s = Topic title',
'selena_network'
),
$topic_author_name,
$topic_title
);
if ( empty( $subject ) ) {
return;
}
// Loop through users
foreach ( (array) $user_ids as $user_id ) {
// Don't send notifications to the person who made the post
if ( !empty( $topic_author ) && (int) $user_id === (int) $topic_author ) {
continue;
}
// For plugins to filter messages per reply/topic/user
$message = sprintf( _x( '%1$s created new conversation “%2$s” in the forum “%3$s” %4$s.
+
Notification settings → %5$s.
Send email at %6$s or respond on that letter if you have a questions.',
'%1$s = Topic author name
%2$s = Created topic title
%3$s = Forum title in wich topic created
%4$s = Link to brand new topic
%5$s = URL to manage user subscribtions
%6$s = Admin email',
'selena_network' ),
$topic_author_name, // 1
$topic_title, // 2
$forum_title, // 3
$topic_url, // 4
bbp_get_subscriptions_permalink ($user_id), // 5
get_option ('admin_email') // 6
);
if ( empty( $message ) ) {
continue;
}
// Send notification email
wp_mail (
get_userdata( $user_id )->user_email,
$subject,
$message
);
}
return true;
}
?>