Social networks run on notifications and BuddyPress helps make that happen. BuddyPress sends out notifications for various actions on the site, like when you get a message from a friend or when someone replies to your status update. Though BuddyPress sends out quite a few notifications, you might want to add custom notifications for other actions. For example, you may want to send out a notification when a friends birthday is near.

In this tutorial, we will go over the code necessary to send a member a notification when someone comments on their blog post. You don’t need a lot of code to add notifications; they just require special formatting.

The first thing required is that notifications are attached to a BuddyPress Component. No worries though–you do not actually have to create a full component! It just needs to be registered by adding to an array. Adding a “fake” component to the registered component array is done by adding a filter to bp_notifications_get_registered_components. In the code snip below we are adding a fake component called “custom”. You can name this fake component anything. We will use this name later when adding the notification.

  1. function custom_filter_notifications_get_registered_components( $component_names = array() ) {
  3. // Force $component_names to be an array
  4. if ( ! is_array( $component_names ) ) {
  5. $component_names = array();
  6. }
  8. // Add ‘custom’ component to registered components array
  9. array_push( $component_names, ‘custom’ );
  11. // Return component’s with ‘custom’ appended
  12. return $component_names;
  13. }
  14. add_filter( ‘bp_notifications_get_registered_components’, ‘custom_filter_notifications_get_registered_components’ );

Once we have the fake component registered, we need to create a function to format what is actually displayed on the members notification table list. In the image below you can see the column “Notification”. The content of this box is what we need to format. This should be a short message to explain the notification and to link back to a piece of content. In this case, it’s the comment link:


There are a couple caveats when formatting this callback function. We need to take into account the admin bar markup. We need to pass back a string and also an array. Formatted as such:

  1. // WordPress Toolbar
  2. if ( ‘string’ === $format ) {
  3. $return = apply_filters( ‘custom_filter’, ‘<a href=”‘ . esc_url( $custom_link ) . ‘” title=”‘ . esc_attr( $custom_title ) . ‘”>’ . esc_html( $custom_text ) . ‘</a>’, $custom_text, $custom_link );
  5. // Deprecated BuddyBar
  6. } else {
  7. $return = apply_filters( ‘custom_filter’, array(
  8. ‘text’ => $custom_text,
  9. ‘link’ => $custom_link
  10. ), $custom_link, (int) $total_items, $custom_text, $custom_title );
  11. }

The other caveat is adding a notification doesn’t really add anything until it is displayed. The only thing that is saved is the item id (what you want to link to). In our code we are doing notifications for comments, so we save the comment id and then during the call back formatting function get the required data to display. See the code below where we get the $item_id and use it with get_comment() to compile whats shown in that Notification box in the table list. The $item_id is set during the action that adds the notification. The $custom_text can be customized to fit the content you are linking to.

The format code needs to be wrapped in an action check; if this is not done it will reformat every notification. Note: Notifications are built each time they are displayed. In the code example I have set the action to be ‘custom_action’. You can name this anything–when adding the notification later you specify this action.

  1. function custom_format_buddypress_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = ‘string’ ) {
  3. // New custom notifications
  4. if ( ‘custom_action’ === $action ) {
  5. $comment = get_comment( $item_id );
  6. $custom_title = $comment->comment_author . ‘ commented on the post ‘ . get_the_title( $comment->comment_post_ID );
  7. $custom_link = get_comment_link( $comment );
  8. $custom_text = $comment->comment_author . ‘ commented on your post ‘ . get_the_title( $comment->comment_post_ID );
  10. // WordPress Toolbar
  11. if ( ‘string’ === $format ) {
  12. $return = apply_filters( ‘custom_filter’, ‘<a href=”‘ . esc_url( $custom_link ) . ‘” title=”‘ . esc_attr( $custom_title ) . ‘”>’ . esc_html( $custom_text ) . ‘</a>’, $custom_text, $custom_link );
  14. // Deprecated BuddyBar
  15. } else {
  16. $return = apply_filters( ‘custom_filter’, array(
  17. ‘text’ => $custom_text,
  18. ‘link’ => $custom_link
  19. ), $custom_link, (int) $total_items, $custom_text, $custom_title );
  20. }
  21. return $return;
  22. }
  23. }
  24. add_filter( ‘bp_notifications_get_notifications_for_user’, ‘custom_format_buddypress_notifications’, 10, 5 );

Now we need to hook to an action to run the bp_notifications_add_notification() function and save that item id. You can hook to almost any action for the type of notification you want to send. That birthday notification example mentioned at the beginning of this tutorial could be hooked to run on login and then cycle through friend ids and then when it finds a friend with a birthday in a set time, save that user id and then the format callback function can retrieve that friends id and display an appropriate notification.

In our case we are hooking to wp_insert_comment. This hook allows us to get the comment id to save as the item id to be used in the format callback function. You also need to provide the user id of who will be receiving this notification.

In this case, we want to notify the post author. We can tap into the wp_insert_comment hook, as it has the comment object and provides the post id the comment is getting attached to. Then we can get the author id from the get_post() response. You will see an argument for ‘component_name’ and ‘component_action’. The ‘component_name’ is the fake component we registered in custom_filter_notifications_get_registered_components(). The ‘component_action’ is the action we wrapped around our formatting code earlier. This is so the format code only runs on specific notifications.

  1. function bp_custom_add_notification( $comment_id, $comment_object ) {
  3. $post = get_post( $comment_object->comment_post_ID );
  4. $author_id = $post->post_author;
  6. bp_notifications_add_notification( array(
  7. ‘user_id’ => $author_id,
  8. ‘item_id’ => $comment_id,
  9. ‘component_name’ => ‘custom’,
  10. ‘component_action’ => ‘custom_action’,
  11. ‘date_notified’ => bp_core_current_time(),
  12. ‘is_new’ => 1,
  13. ) );
  14. }
  15. add_action( ‘wp_insert_comment’, ‘bp_custom_add_notification’, 99, 2 );

Thats about it–only three functions to get custom notifications in BuddyPress. Click here for the full code in this tutorial. If you got any questions, post in the comments and I’ll get notified…see what I did there?