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
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 * * @see https://bbpress.trac.wordpress.org/ticket/2620 */ $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
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.