In this tutorial, you will learn how to register your own BuddyBoss Platform notifications that will trigger based on the criteria you specify. Once registered, you’ll be able to enable and disable them in the “Notification Types” section of the WordPress Admin Dashboard > BuddyBoss > Settings > Notifications page and the “Notification Preferences” section on the Account Settings > Notification Preferences page.
Note: The tutorial is intended for registering and migrating custom notifications to Modern Notifications API from the Legacy Notifications API & Legacy Email Preferences API.
To start, you will need to extend our abstract class by following these steps:
1. Create one class file and extend BP_Core_Notification_Abstract
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 | class BP_Custom_Notification extends BP_Core_Notification_Abstract { /** * Instance of this class. * * @var object */ private static $instance = null; /** * Get the instance of this class. * * @return null|BP_Custom_Notification|Controller|object */ public static function instance() { if ( is_null ( self:: $instance ) ) { self:: $instance = new self(); } return self:: $instance ; } } |
2. Extend two abstract methods into the newly created class from Step 1.
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | /** * Set up the Custom notification class. */ class BP_Custom_Notification extends BP_Core_Notification_Abstract { /** * Instance of this class. * * @var object */ private static $instance = null; /** * Get the instance of this class. * * @return null|BP_Custom_Notification|Controller|object */ public static function instance() { if ( is_null ( self:: $instance ) ) { self:: $instance = new self(); } return self:: $instance ; } /** * Constructor method. */ public function __construct() { $this ->start(); } /** * Initialize all methods inside it. * * @return mixed|void */ public function load() {} /** * Format the notifications. * * @param string $content Notification content. * @param int $item_id Notification item ID. * @param int $secondary_item_id Notification secondary item ID. * @param int $action_item_count Number of notifications with the same action. * @param string $component_action_name Canonical notification action. * @param string $component_name Notification component ID. * @param int $notification_id Notification ID. * @param string $screen Notification Screen type. * * @return array */ public function format_notification( $content , $item_id , $secondary_item_id , $action_item_count , $component_action_name , $component_name , $notification_id , $screen ) { return $content ; } } |
3. Register a group for your platform notifications. This will help manage similar kinds of notifications in one section on the Settings screen.
01 02 03 04 05 06 07 08 09 10 11 12 13 | /** * Register Notification Group. * * @param string $group_key Group key. * @param string $group_label Group label. * @param string $group_admin_label Group admin label. * @param int $priority Priority of the group. */ $this ->register_notification_group( 'custom' , esc_html__( 'Custom Notification Frontend' , 'buddyboss' ), // For the frontend. esc_html__( 'Custom Notifications Admin' , 'buddyboss' ) // For the backend. ); |
4. Once a group has been registered, Register the Platform Notification Type into the created group using this method:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 | /** * Register Notification Type. * * @param string $notification_type Notification Type key. * @param string $notification_label Notification label. * @param string $notification_admin_label Notification admin label. * @param string $notification_group Notification group. * @param bool $default Default status for enabled/disabled. */ $this ->register_notification_type( 'notification_custom' , esc_html__( 'Custom Notification title' , 'buddyboss' ), esc_html__( 'Custom Notification admin title' , 'buddyboss' ), 'custom' ); |
5. Register the Platform Email Type and attach it to the Registered Platform Notification Type using this method.
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 | /** * Add email schema. * * @param string $email_type Type of email being sent. * @param array $args Email arguments. * @param string $notification_type Notification Type key. */ $this ->register_email_type( 'custom-at-message' , array ( 'email_title' => __( 'email title' , 'buddyboss' ), 'email_content' => __( 'email content' , 'buddyboss' ), 'email_plain_content' => __( 'email plain text content' , 'buddyboss' ), 'situation_label' => __( 'Email situation title' , 'buddyboss' ), 'unsubscribe_text' => __( 'You will no longer receive emails when custom notification performed.' , 'buddyboss' ), ), 'notification_custom' ); |
6. Register a Platform Notification and attach it to the Registered Platform Notification Type using the following method.
01 02 03 04 05 06 07 08 09 10 11 12 13 | /** * Register notification. * * @param string $component Component name. * @param string $component_action Component action. * @param string $notification_type Notification Type key. * @param string $icon_class Notification Small Icon. */ $this ->register_notification( 'custom' , 'custom_action' , 'notification_custom' ); |
7. If you want to show notification registered above into the notification filter on the member’s notification list use the following method:
01 02 03 04 05 06 07 08 09 10 11 12 | /** * Register Notification Filter. * * @param string $notification_label Notification label. * @param array $notification_types Notification types. * @param int $notification_position Notification position. */ $this ->register_notification_filter( __( 'Custom Notification Filter' , 'buddyboss' ), array ( 'notification_custom' ), 5 ); |
8. You can load your custom notification class from your child theme using the below hooks.
01 02 03 04 05 06 07 08 09 10 11 12 | // Include custom notification file. require_once trailingslashit( get_template_directory() ) . 'class-bp-custom-notification.php' ; add_action( 'bp_init' , function () { // Register custom notification in preferences screen. if ( class_exists ( 'BP_Custom_Notification' ) ) { BP_Custom_Notification::instance(); } } ); |
9. In the next step you need to provide the support for rendering of notification registered above inside a method called format_notification
see the example below.
Tip: Make use the of $screen variable to return different content for notifications that are displayed on different devices or screens.
$screen variables is an enum of
web
refers to the notifications list page or notifications popup on the site.web_push
refers to the browser’s native push notifications popup.app_push
refers to the mobile device’s native notification center.
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | public function format_notification( $content , $item_id , $secondary_item_id , $action_item_count , $component_action_name , $component_name , $notification_id , $screen ) { if ( 'custom' === $component_name && 'custom_action' === $component_action_name ) { $text = esc_html__( 'Custom Notification text.' , 'buddyboss' ); $link = get_permalink( $item_id ); /** * Change the text for Push Notifications */ if ( $screen == "app_push" || $screen == "web_push" ) { $text = esc_html__( 'Custom Push Notification Text Only.' , 'buddyboss' ); } return array ( 'title' => "" , // (optional) only for push notification & if not provided no title will be used. 'text' => $text , 'link' => $link , ); } return $content ; } |
Note: Make sure to add a comparison of $component_name & $component_action_name before returning any alteration of $content as wrongly handling this method can affect the issues on other notification rendering on the user’s notifications list page.
Tip: Make use of $screen
argument to provide a different modified content for web & push interface by returning the alternate versions comparing variable with (web, app_push, web_push).
Tip: To support title inside a push notification you need to profile the title inside $content array variable which is optional.
Note: For Push Notifications, the maximum number of characters for the title is 26 and message content is 354.
Full Example Code
01 02 03 04 05 06 07 08 09 10 11 12 | // Include custom notification file. require_once trailingslashit( get_template_directory() ) . 'class-bp-custom-notification.php' ; add_action( 'bp_init' , function () { // Register custom notification in preferences screen. if ( class_exists ( 'BP_Custom_Notification' ) ) { BP_Custom_Notification::instance(); } } ); |
Example class file for custom notification:
class-bp-custom-notification.php
This file also goes into your plugin folder. This contains most of the functional code for registering and migrating your custom notifications to Modern Notifications API.
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | <?php /** * BuddyBoss Custom Notification Class. */ defined( 'ABSPATH' ) || exit ; if ( ! class_exists ( 'BP_Core_Notification_Abstract' ) ) { return ; } /** * Set up the Custom notification class. */ class BP_Custom_Notification extends BP_Core_Notification_Abstract { /** * Instance of this class. * * @var object */ private static $instance = null; /** * Get the instance of this class. * * @return null|BP_Custom_Notification|Controller|object */ public static function instance() { if ( is_null ( self:: $instance ) ) { self:: $instance = new self(); } return self:: $instance ; } /** * Constructor method. */ public function __construct() { $this ->start(); } /** * Initialize all methods inside it. * * @return mixed|void */ public function load() { /** * Register Notification Group. * * @param string $group_key Group key. * @param string $group_label Group label. * @param string $group_admin_label Group admin label. * @param int $priority Priority of the group. */ $this ->register_notification_group( 'custom' , esc_html__( 'Custom Notification Frontend' , 'buddyboss' ), // For the frontend. esc_html__( 'Custom Notifications Admin' , 'buddyboss' ) // For the backend. ); $this ->register_custom_notification(); } /** * Register notification for user mention. */ public function register_custom_notification() { /** * Register Notification Type. * * @param string $notification_type Notification Type key. * @param string $notification_label Notification label. * @param string $notification_admin_label Notification admin label. * @param string $notification_group Notification group. * @param bool $default Default status for enabled/disabled. */ $this ->register_notification_type( 'notification_custom' , esc_html__( 'Custom Notification title' , 'buddyboss' ), esc_html__( 'Custom Notification admin title' , 'buddyboss' ), 'custom' ); /** * Add email schema. * * @param string $email_type Type of email being sent. * @param array $args Email arguments. * @param string $notification_type Notification Type key. */ $this ->register_email_type( 'custom-at-message' , array ( 'email_title' => __( 'email title' , 'buddyboss' ), 'email_content' => __( 'email content' , 'buddyboss' ), 'email_plain_content' => __( 'email plain text content' , 'buddyboss' ), 'situation_label' => __( 'Email situation title' , 'buddyboss' ), 'unsubscribe_text' => __( 'You will no longer receive emails when custom notification performed.' , 'buddyboss' ), ), 'notification_custom' ); /** * Register notification. * * @param string $component Component name. * @param string $component_action Component action. * @param string $notification_type Notification Type key. * @param string $icon_class Notification Small Icon. */ $this ->register_notification( 'custom' , 'custom_action' , 'notification_custom' ); /** * Register Notification Filter. * * @param string $notification_label Notification label. * @param array $notification_types Notification types. * @param int $notification_position Notification position. */ $this ->register_notification_filter( __( 'Custom Notification Filter' , 'buddyboss' ), array ( 'notification_custom' ), 5 ); } /** * Format the notifications. * * @param string $content Notification content. * @param int $item_id Notification item ID. * @param int $secondary_item_id Notification secondary item ID. * @param int $action_item_count Number of notifications with the same action. * @param string $component_action_name Canonical notification action. * @param string $component_name Notification component ID. * @param int $notification_id Notification ID. * @param string $screen Notification Screen type. * * @return array */ public function format_notification( $content , $item_id , $secondary_item_id , $action_item_count , $component_action_name , $component_name , $notification_id , $screen ) { if ( 'custom' === $component_name && 'custom_action' === $component_action_name ) { $text = esc_html__( 'Custom Notification text.' , 'buddyboss' ); $link = get_permalink( $item_id ); /** * Change the text for Push Notifications */ if ( $screen == "app_push" || $screen == "web_push" ) { $text = esc_html__( 'Custom Push Notification Text Only.' , 'buddyboss' ); } return array ( 'title' => "" , // (optional) only for push notification & if not provided no title will be used. 'text' => $text , 'link' => $link , ); } return $content ; } } |