Skip to:
Content
Pages
Categories
Search
Top
Bottom

bp custom notifications proper guide


  • ah72king
    Participant

    @ah72king

    I have custom plugin in which i have review approved and delete functions so when i do some sort of bulk action like delete or i approved a review.
    i want to generate a bp custom notifications at that user dashboard or member page which review was approved or deleted
    i place this where i perform approved action

    do_action('review-approved');

    Then i make function below


    function bp_review_add_notification() {
    if ( bp_is_active( 'notifications' ) ) {
    bp_notifications_add_notification( array(
    'user_id' => 1, // this is one because for testing just admin now
    'component_name' => 'review',
    'component_action' => 'review_approved',
    'date_notified' => bp_core_current_time(),
    'is_new' => 1,
    ) );
    }
    }
    add_action( 'review-approved', 'bp_review_add_notification',10,5);

    Review is added in database i checked that but it is not shown on dashboard or member page
    how to do that

    or can you refer any complete guide to create a custom notification upon some action

Viewing 1 replies (of 1 total)

  • ah72king
    Participant

    @ah72king

    I found a solution that can help

    
    <?php
    // For Custom Notification 
    // Registering Custom Componet 
    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' );
    
    // Formatting custom with respect to action
    function bp_custom_format_buddypress_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) {
     		
     		//$item_id this your id which you can use to get your respected data
        	$data  = get_my_values_custom_function($item_id); // this is custom function it depend on your needs and data
        	$custom_title = $data->title;
        	$custom_link  = $data->link;
        	$custom_text  = $data->text;
    
        // New custom notifications
        if ( 'custom_action' === $action ) {
            
            // WordPress Toolbar
            if ( 'string' === $format ) {
                $return = apply_filters( 'custom_filter','Your custom notification for <a href="'.$custom_link.'">'.$custom_title.'</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', 'bp_custom_format_buddypress_notifications', 10, 5 );
    
    // Adding custom Notification in DB 
    function bp_custom_notification( $item_id, $author_id ) {
    
        if ( bp_is_active( 'notifications' ) ) {   // if notification is active from admin panel
        	// if notification is active from admin panel bp_notifications_add_notification function to add notification into database
            bp_notifications_add_notification( array(                        
                'user_id'           => $author_id, // User to whom notification has to be send
                'item_id'           => $item_id,  // Id of thing you want to show it can be item_id or post or custom post or anything
                'component_name'    => 'custom', //  component that we registered
                'component_action'  => 'custom_action', // Our Custom Action 
                'date_notified'     => bp_core_current_time(), // current time
                'is_new'            => 1, // It say that is new notification
            ) );
        }
    }
    add_action( 'custom_hooks', 'bp_custom_notification', 10, 2);
    /**
    * custom_hooks is action name which will be call by do_action() function
    * bp_custom_notification your function name
    * 10 is priority number
    * 2 is number of parameter 
    */
    
    /****
    * Now Where to call custom_hooks and how 
    */
    
    do_action('custom_hooks', $item_id, $author_id );
    
    /****
    * place it where you want to call this action and pass parameter
    */ 
    
Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.
Skip to toolbar