Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a custom notification in buddypress

I would like to add a custom notification to my buddypress "notification" tab when a particular event occurs. How to achieve this?

like image 449
karthi Avatar asked Apr 18 '14 06:04

karthi


Video Answer


2 Answers

I followed this tutorial and is very complete. Worked for me

BuddyPress: Adding Custom Notifications

I am going to put that the author wrote. But is better if you go to the post directly, there you can find a much better explanation. I think that the post is for dummies, very complete and explanatory, even has a gist.

1st register your component

You need to register your notification as a budypress component. This is very easy. The name of the component registered was custom

// this is to add a fake component to BuddyPress. A registered component is needed to add notifications
function custom_filter_notifications_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_get_registered_components' );

2nd Render the notification

// this gets the saved item id, compiles some data and then displays the notification
function custom_format_buddypress_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) {
    // New custom notifications
    if ( 'custom_action' === $action ) {

        $comment = get_comment( $item_id );

        $custom_title = $comment->comment_author . ' commented on the post ' . get_the_title( $comment->comment_post_ID );
        $custom_link  = get_comment_link( $comment );
        $custom_text = $comment->comment_author . ' commented on your post ' . get_the_title( $comment->comment_post_ID );
        // WordPress Toolbar
        if ( 'string' === $format ) {
            $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 );
        // 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', 'custom_format_buddypress_notifications', 10, 5 );

3st Launch the Notification

Here you add the notification when someone writes you on a post. Use the action hook wp_insert_comment for catch that event.

// this hooks to comment creation and saves the comment id
function bp_custom_add_notification( $comment_id, $comment_object ) {
    $post = get_post( $comment_object->comment_post_ID );
    $author_id = $post->post_author;
    bp_notifications_add_notification( array(
        'user_id'           => $author_id,
        'item_id'           => $comment_id,
        'component_name'    => 'custom',
        'component_action'  => 'custom_action',
        'date_notified'     => bp_core_current_time(),
        'is_new'            => 1,
    ) );

}
add_action( 'wp_insert_comment', 'bp_custom_add_notification', 99, 2 );

All together

<?php
// this is to add a fake component to BuddyPress. A registered component is needed to add notifications
function custom_filter_notifications_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_get_registered_components' );


// this gets the saved item id, compiles some data and then displays the notification
function custom_format_buddypress_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) {

    // New custom notifications
    if ( 'custom_action' === $action ) {

        $comment = get_comment( $item_id );

        $custom_title = $comment->comment_author . ' commented on the post ' . get_the_title( $comment->comment_post_ID );
        $custom_link  = get_comment_link( $comment );
        $custom_text = $comment->comment_author . ' commented on your post ' . get_the_title( $comment->comment_post_ID );

        // WordPress Toolbar
        if ( 'string' === $format ) {
            $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 );

        // 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', 'custom_format_buddypress_notifications', 10, 5 );


// this hooks to comment creation and saves the comment id
function bp_custom_add_notification( $comment_id, $comment_object ) {

    $post = get_post( $comment_object->comment_post_ID );
    $author_id = $post->post_author;

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

}
add_action( 'wp_insert_comment', 'bp_custom_add_notification', 99, 2 );
like image 183
Diego Juliao Avatar answered Oct 23 '22 10:10

Diego Juliao


You use bp_notifications_add_notification(). The following example function is hooked to bp_activity_sent_mention_email - So when an email notification is sent due to somebody being @-mentioned, a core notification is generated.

function bp_activity_at_mention_add_notification( $activity, $subject, $message, $content, $receiver_user_id ) {
    if ( bp_is_active( 'notifications' ) ) {
        bp_notifications_add_notification( array(
            'user_id'           => $receiver_user_id,
            'item_id'           => $activity->id,
            'secondary_item_id' => $activity->user_id,
            'component_name'    => buddypress()->activity->id,
            'component_action'  => 'new_at_mention',
            'date_notified'     => bp_core_current_time(),
            'is_new'            => 1,
        ) );
    }
}
add_action( 'bp_activity_sent_mention_email', 'bp_activity_at_mention_add_notification', 10, 5 );

Ref: http://codex.buddypress.org/developer/function-examples/bp_notifications_add_notification/

like image 38
henrywright Avatar answered Oct 23 '22 12:10

henrywright