Posted on

How To Create A Custom BuddyPress Notifications While Posting a New Post

BuddyPress has own notification system. It add notification for all default component.It notify members of relevant activity with a toolbar bubble and/or via email.

Suppose we need notification for some custom action or custom BuddyPress Action. Then also BuddyPress gives option for it. It has bp_notifications_add_notification function.

bp_notifications_add_notification( array(
            'user_id'           => $author_id,
            'item_id'           => $post_id,
            'secondary_item_id' => $author_id,
            'component_name'    => 'custom',
            'component_action'  => 'custom_action',
            'date_notified'     => bp_core_current_time(),
            'is_new'            => 1,
) );

For more details, Please check here.

Before that we need to do some thing. We need to register the notification component. We need to use bp_notifications_get_registered_components. Let’s our component name is ‘custom’.

function custom_filter_notifications_publish_post_get_registered_components( $component_names = array() ) {
    // Force $component_names to be an array
    if ( ! is_array( $component_names ) ) {
        $component_names = array();
    // Add 'custom' component to registered components array
    array_push( $component_names, 'custom' );
    // Return component's with 'custom' appended
    return $component_names;
add_filter( 'bp_notifications_get_registered_components', 'custom_filter_notifications_publish_post_get_registered_components' );

Now we need to format the notification.For that we use bp_notifications_get_notifications_for_user

function bp_custom_format_buddypress_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) {
    // New custom notifications
    if ( 'custom_action' === $action ) {
        $post_info = get_post( $item_id );

        $custom_title = get_the_author_meta( 'display_name', $post_info->post_author ) . ' posted a new post ' . get_the_title( $item_id );
        $custom_link  = get_post_permalink( $item_id );
        $custom_text = get_the_author_meta( 'display_name', $post_info->post_author ) . ' posted a new post ' . get_the_title( $item_id );
        // WordPress Toolbar
        if ( 'string' === $format ) {
            $return = apply_filters( 'custom_filter', '' . esc_html( $custom_text ) . '', $custom_text, $custom_link );
        // Deprecated BuddyBar
        } else {
            $return = apply_filters( 'custom_filter', array(
                'text' => $custom_text,
                'link' => $custom_link
            ), $custom_link, (int) $total_items, $custom_text, $custom_title );
        return $return;
add_filter( 'bp_notifications_get_notifications_for_user', 'bp_custom_format_buddypress_notifications', 10, 5 );

Now in the we need to use bp_notifications_add_notification for add notification. Here I have explained how to add notification when new post is published. So I have used publish_post.

function bp_post_published_notification( $post_id, $post ) {
    $author_id = $post->post_author; /* Post author ID. */
    if ( bp_is_active( 'notifications' ) ) {
	    bp_notifications_add_notification( array(
	        'user_id'           => $author_id,
	        'item_id'           => $post_id,
	        'component_name'    => 'custom',
	        'component_action'  => 'custom_action',
	        'date_notified'     => bp_core_current_time(),
	        'is_new'            => 1,
	    ) );
add_action( 'publish_post', 'bp_post_published_notification', 99, 2 );

Depending our need, we can hook it to different action. For example, here BP Notification For New Comments.


Click here for the full code in this tutorial.

7 comments How To Create A Custom BuddyPress Notifications While Posting a New Post

  1. hi, i am serching this code everywhere but i need the code for email notification of post author activity only to his friend in buddypress. If a post author publish an update from activty or from a custom post is it possible that read the email notification only his friend? Please help me

  2. Yes possible.
    $author_id = $friend_id; /* friend ID. */
    bp_notifications_add_notification( array(
    ‘user_id’ => $friend_id,
    ‘item_id’ => $post_id,
    ‘component_name’ => ‘custom’,
    ‘component_action’ => ‘custom_action’,
    ‘date_notified’ => bp_core_current_time(),
    ‘is_new’ => 1,
    ) );

    We need to pass user friends id. We need to put in loop for multiple friends . user_id should be ‘user_id’ => $friend_id,

    1. You are a genius, so i can insert this code in bp custom file? What mean with this?
      We need to put in loop for multiple friends . user_id should be ‘user_id’ => $friend_id,

      For sure the post author have multiply friends, so where i have to insert this latest code? Sorry i am not expert

      1. You need to fetch the $friend_id. It is not the actual code 🙂
        You need to fetch user friend ids.

          1. Ok i see but for example if i have 3 friends, you have 10 friends, there is a global solution for send the new post email notification only to all friend_id of the authors? Can you do for me? I pay a fee, please

Comments are closed.