Skip to:
Content
Pages
Categories
Search
Top
Bottom

Custom Notifications BuddyPress


  • tarunweb1
    Participant

    @tarunweb1

    Hello Guys,

    I am creating a post subscription. When any user subscribe to any post I want to send them notifications about comments and post activities. I am adding custom notifications to buddypress. Notifications are adding but in frontend the title of notification is not showing. I checked many forums but did not find any solution.

    Below is my code:

    
    public function __construct() {
          
          add_filter( 'bp_notifications_get_registered_components', array($this,'custom_filter_notifications_get_registered_components'),20 );
          add_filter( 'bp_notifications_get_notifications_for_user', array($this,'custom_format_buddypress_notifications'), 10, 7 );
          add_action( 'wp_insert_comment', array($this,'bp_custom_add_notification'), 99, 2 );
       }
    
    For custom component
    
    public 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;
       }
    
    For notification formatting
    
    public function custom_format_buddypress_notifications( $content, $item_id, $secondary_item_id, $total_items, $format = 'string', $action, $component ) {
         	   
             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 ) {
                   $data_to_return = apply_filters_ref_array( '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 {
                   $data_to_return = apply_filters_ref_array( 'custom_filter', array(
                       'text' => $custom_text,
                       'link' => $custom_link
                   ), $custom_link, (int) $total_items, $item_id, $secondary_item_id );
               }
               
             return $data_to_return;     
          } else{
             return $action;
          } 
       }
    
    For adding notification
    
    public function bp_custom_add_notification( $comment_id, $comment_object ) {
     
          $subscribed_user =  get_post_meta( $comment_object->comment_post_ID , 'subscribed_user',true ); 
          
          if(!empty($subscribed_user) && is_array($subscribed_user)){
             for ($i=0; $i < count($subscribed_user); $i++) { 
          		 if ( bp_is_active( 'notifications' ) ) {
                      bp_notifications_add_notification( array(
                         'user_id'           => $subscribed_user[$i],
                         'item_id'           => $comment_id,
                         'component_name'    => 'custom',
                         'component_action'  => 'custom_action',
                         'date_notified'     => bp_core_current_time(),
                         'is_new'            => 1,
                       ) );
          		 }
             }
          }   
       }
    
    

    Please suggest best possible way to fix this issue.

    Thank You

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.
Skip to toolbar