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

(int) (Required) ID of the newly made reply

$topic_id

(int) (Required) ID of the topic of the reply

$forum_id

(int) (Required) ID of the forum of the reply

$anonymous_data

(mixed) (Optional) Array of anonymous user data

Default value: false

$reply_author

(int) (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_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.