bbp_notify_topic_subscribers( int $reply_id, int $topic_id, int $forum_id, mixed $anonymous_data = false, int $reply_author )

Sends notification emails for new replies to subscribed topics

Description

Gets new post’s ID and check if there are subscribed users to that topic, and if there are, send notifications

Note: in bbPress 2.6, we’ve moved away from 1 email per subscriber to 1 email with everyone BCC’d. This may have negative repercussions for email services that limit the number of addresses in a BCC field (often to around 500.) In those cases, we recommend unhooking this function and creating your own custom emailer script.

Parameters

$reply_id

(Required) ID of the newly made reply

$topic_id

(Required) ID of the topic of the reply

$forum_id

(Required) ID of the forum of the reply

$anonymous_data

(Optional) Array of anonymous user data

Default value: false

$reply_author

(Required) ID of the topic author ID

Return

(bool) True on success, false on failure

Source

File: bp-forums/common/functions.php

1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
function bbp_notify_topic_subscribers( $reply_id = 0, $topic_id = 0, $forum_id = 0, $anonymous_data = false, $reply_author = 0 ) {
 
    // Bail if subscriptions are turned off
    if ( !bbp_is_subscriptions_active() ) {
        return false;
    }
 
    /** Validation ************************************************************/
 
    $reply_id = bbp_get_reply_id( $reply_id );
    $topic_id = bbp_get_topic_id( $topic_id );
    $forum_id = bbp_get_forum_id( $forum_id );
 
    /** Topic *****************************************************************/
 
    // Bail if topic is not published
    if ( !bbp_is_topic_published( $topic_id ) ) {
        return false;
    }
 
    /** Reply *****************************************************************/
 
    // Bail if reply is not published
    if ( !bbp_is_reply_published( $reply_id ) ) {
        return false;
    }
 
    // Poster name
    $reply_author_name = bbp_get_reply_author_display_name( $reply_id );
 
    /** Mail ******************************************************************/
 
    // 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 ) );
    $topic_url     = get_permalink( $topic_id );
    $reply_content = strip_tags( bbp_get_reply_content( $reply_id ) );
    $reply_url     = bbp_get_reply_url( $reply_id );
 
    $forum_title   = wp_strip_all_tags( get_post_field( 'post_title', $forum_id ) );
    $forum_url     = esc_url( bbp_get_forum_permalink( $forum_id ) );
 
    $args = array(
        'tokens' => array(
            'forum.title'      => $forum_title,
            'forum.url'        => $forum_url,
            'discussion.title' => $topic_title,
            'discussion.url'   => $topic_url,
            'reply.id'         => $reply_id,
            'reply.url'        => $reply_url,
            'reply.content'    => $reply_content,
            'poster.name'      => $reply_author_name,
        ),
    );
 
    // Get topic subscribers and bail if empty
    $user_ids = bbp_get_topic_subscribers( $topic_id, true );
 
    // Dedicated filter to manipulate user ID's to send emails to
    $user_ids = apply_filters( 'bbp_topic_subscription_user_ids', $user_ids );
    if ( empty( $user_ids ) ) {
        return false;
    }
 
    /** Send it ***************************************************************/
 
    do_action( 'bbp_pre_notify_subscribers', $reply_id, $topic_id, $user_ids );
 
    // 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;
        }
 
        // Send notification email.
        bp_send_email( 'bbp-new-forum-reply', (int) $user_id, $args );
    }
 
    do_action( 'bbp_post_notify_subscribers', $reply_id, $topic_id, $user_ids );
 
    return true;
}

Changelog

Changelog
Version Description
bbPress (r5413) Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.