bbp_notify_forum_subscribers

Sends notification emails for new topics to subscribed forums.

Description

bbp_notify_forum_subscribers( (int) $topic_id = 0, (int) $forum_id = 0, (constant) $anonymous_data = false, (int) $topic_author = 0 ); 

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 (4)

0. $topic_id — Optional. (int)
ID of the newly made reply
1. $forum_id — Optional. (int)
ID of the forum for the topic
2. $anonymous_data — Optional. (constant) => false
Array of anonymous user data
3. $topic_author — Optional. (int)
ID of the topic author ID

Usage

  1. if ( !function_exists( 'bbp_notify_forum_subscribers' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'bbpress/includes/common/functions.php'; 
  3.  
  4. // ID of the newly made reply 
  5. $topic_id = -1; 
  6.  
  7. // ID of the forum for the topic 
  8. $forum_id = -1; 
  9.  
  10. // Array of anonymous user data 
  11. $anonymous_data = false; 
  12.  
  13. // ID of the topic author ID 
  14. $topic_author = -1; 
  15.  
  16. // NOTICE! Understand what this does before running. 
  17. $result = bbp_notify_forum_subscribers($topic_id, $forum_id, $anonymous_data, $topic_author); 
  18.  

Defined (1)

The function is defined in the following location(s).

/includes/common/functions.php  
  1. function bbp_notify_forum_subscribers( $topic_id = 0, $forum_id = 0, $anonymous_data = false, $topic_author = 0 ) { 
  2.  
  3. // Bail if subscriptions are turned off 
  4. if ( !bbp_is_subscriptions_active() ) { 
  5. return false; 
  6.  
  7. /** Validation ************************************************************/ 
  8.  
  9. $topic_id = bbp_get_topic_id( $topic_id ); 
  10. $forum_id = bbp_get_forum_id( $forum_id ); 
  11.  
  12. /** 
  13. * Necessary for backwards compatibility 
  14. * @see https://bbpress.trac.wordpress.org/ticket/2620 
  15. */ 
  16. $user_id = 0; 
  17.  
  18. /** Topic *****************************************************************/ 
  19.  
  20. // Bail if topic is not published 
  21. if ( ! bbp_is_topic_published( $topic_id ) ) { 
  22. return false; 
  23.  
  24. // Poster name 
  25. $topic_author_name = bbp_get_topic_author_display_name( $topic_id ); 
  26.  
  27. /** Mail ******************************************************************/ 
  28.  
  29. // Remove filters from reply content and topic title to prevent content 
  30. // from being encoded with HTML entities, wrapped in paragraph tags, etc... 
  31. remove_all_filters( 'bbp_get_topic_content' ); 
  32. remove_all_filters( 'bbp_get_topic_title' ); 
  33.  
  34. // Strip tags from text and setup mail data 
  35. $topic_title = strip_tags( bbp_get_topic_title( $topic_id ) ); 
  36. $topic_content = strip_tags( bbp_get_topic_content( $topic_id ) ); 
  37. $topic_url = get_permalink( $topic_id ); 
  38. $blog_name = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); 
  39.  
  40. // For plugins to filter messages per reply/topic/user 
  41. $message = sprintf( __( '%1$s wrote: 
  42.  
  43. %2$s 
  44.  
  45. Topic Link: %3$s 
  46.  
  47. ----------- 
  48.  
  49. You are receiving this email because you subscribed to a forum. 
  50.  
  51. Login and visit the topic to unsubscribe from these emails.', bbpress ),  
  52.  
  53. $topic_author_name,  
  54. $topic_content,  
  55. $topic_url 
  56. ); 
  57.  
  58. $message = apply_filters( 'bbp_forum_subscription_mail_message', $message, $topic_id, $forum_id, $user_id ); 
  59. if ( empty( $message ) ) { 
  60. return; 
  61.  
  62. // For plugins to filter titles per reply/topic/user 
  63. $subject = apply_filters( 'bbp_forum_subscription_mail_title', '[' . $blog_name . '] ' . $topic_title, $topic_id, $forum_id, $user_id ); 
  64. if ( empty( $subject ) ) { 
  65. return; 
  66.  
  67. /** User ******************************************************************/ 
  68.  
  69. // Get the noreply@ address 
  70.  
  71. // Setup "From" email address 
  72. $from_email = apply_filters( 'bbp_subscription_from_email', $no_reply ); 
  73.  
  74. // Setup the From header 
  75. $headers = array( 'From: ' . get_bloginfo( 'name' ) . ' <' . $from_email . '>' ); 
  76.  
  77. // Get topic subscribers and bail if empty 
  78. $user_ids = bbp_get_forum_subscribers( $forum_id, true ); 
  79.  
  80. // Dedicated filter to manipulate user ID's to send emails to 
  81. $user_ids = apply_filters( 'bbp_forum_subscription_user_ids', $user_ids ); 
  82. if ( empty( $user_ids ) ) { 
  83. return false; 
  84.  
  85. // Loop through users 
  86. foreach ( (array) $user_ids as $user_id ) { 
  87.  
  88. // Don't send notifications to the person who made the post 
  89. if ( !empty( $topic_author ) && (int) $user_id === (int) $topic_author ) { 
  90. continue; 
  91.  
  92. // Get email address of subscribed user 
  93. $headers[] = 'Bcc: ' . get_userdata( $user_id )->user_email; 
  94.  
  95. /** Send it ***************************************************************/ 
  96.  
  97. // Custom headers 
  98. $headers = apply_filters( 'bbp_subscription_mail_headers', $headers ); 
  99. $to_email = apply_filters( 'bbp_subscription_to_email', $no_reply ); 
  100.  
  101. do_action( 'bbp_pre_notify_forum_subscribers', $topic_id, $forum_id, $user_ids ); 
  102.  
  103. // Send notification email 
  104. wp_mail( $to_email, $subject, $message, $headers ); 
  105.  
  106. do_action( 'bbp_post_notify_forum_subscribers', $topic_id, $forum_id, $user_ids ); 
  107.  
  108. return true;