Skip to content

Instantly share code, notes, and snippets.

@chetansatasiya
Created September 9, 2019 09:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chetansatasiya/d0af3bf22c3674031089e2f180e7b780 to your computer and use it in GitHub Desktop.
Save chetansatasiya/d0af3bf22c3674031089e2f180e7b780 to your computer and use it in GitHub Desktop.
Add Custom Notification
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 'insert_post_comment' component to registered components array
array_push( $component_names, 'insert_post_comment' );
// Return component's with 'insert_post_comment' appended
return $component_names;
}
add_filter( 'bp_notifications_get_registered_components', 'custom_filter_notifications_get_registered_components' );
function custom_format_buddypress_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string', $component_action, $component_name, $id ) {
// New insert_post_comment notifications
if ( 'do_notification_on_comment' === $component_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, 8 );
function bp_custom_add_notification( $comment_id, $comment_object ) {
error_log( $comment_id );
$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' => 'insert_post_comment',
'component_action' => 'do_notification_on_comment',
'date_notified' => bp_core_current_time(),
'is_new' => 1,
) );
}
add_action( 'wp_insert_comment', 'bp_custom_add_notification', 99, 2 );
@chetansatasiya
Copy link
Author

Front View

download1111

@Martin128
Copy link

Yes but if you have more than one notification you can not see them on top in the bar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment