bbp_notify_forum_subscribers( int $topic_id, int $forum_id, mixed $anonymous_data = false, int $topic_author )

Sends notification emails for new topics to subscribed forums

Description

Gets new post’s ID and check if there are subscribed users to that forum, 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

$topic_id

(Required) ID of the newly made reply

$forum_id

(Required) ID of the forum for the topic

$anonymous_data

(Optional) Array of anonymous user data

Default value: false

$topic_author

(Required) ID of the topic author ID

Return

(bool) True on success, false on failure

Source

File: bp-forums/common/functions.php

1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
function bbp_notify_forum_subscribers( $topic_id = 0, $forum_id = 0, $anonymous_data = false, $topic_author = 0 ) {
 
    // Bail if subscriptions are turned off
    if ( !bbp_is_subscriptions_active() ) {
        return false;
    }
 
    /** Validation ************************************************************/
 
    $topic_id = bbp_get_topic_id( $topic_id );
    $forum_id = bbp_get_forum_id( $forum_id );
 
    /**
     * Necessary for backwards compatibility
     *
     */
    $user_id  = 0;
 
    /** Topic *****************************************************************/
 
    // 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 );
 
    /** 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_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 ) );
    $topic_content = strip_tags( bbp_get_topic_content( $topic_id ) );
    $topic_url     = get_permalink( $topic_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.id'      => $topic_id,
            'discussion.title'   => $topic_title,
            'discussion.url'     => $topic_url,
            'discussion.content' => $topic_content,
            'poster.name'        => $topic_author_name,
        ),
    );
 
    // Get topic subscribers and bail if empty
    $user_ids = bbp_get_forum_subscribers( $forum_id, true );
 
    // Dedicated filter to manipulate user ID's to send emails to
    $user_ids = apply_filters( 'bbp_forum_subscription_user_ids', $user_ids );
    if ( empty( $user_ids ) ) {
        return false;
    }
 
    /** Send it ***************************************************************/
 
    do_action( 'bbp_pre_notify_forum_subscribers', $topic_id, $forum_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( $topic_author ) && (int) $user_id === (int) $topic_author ) {
            continue;
        }
 
        // Send notification email.
        bp_send_email( 'bbp-new-forum-topic', (int) $user_id, $args );
    }
 
    do_action( 'bbp_post_notify_forum_subscribers', $topic_id, $forum_id, $user_ids );
 
    return true;
}

Changelog

Changelog
Version Description
bbPress (r5156) 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.