Search code examples
phpwordpressbuddypress

Create custom notification in post insert in buddypress


I want to add custom notification in the time of post creation. I have followed this url https://webdevstudios.com/2015/10/06/buddypress-adding-custom-notifications/

What i have done, I am adding custom notification in creation of project or bid post type. But it not working. Please check my code and let me what i have done wrong.

// 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, 'projectadd' );
    // Return component's with 'custom' appended
    return $component_names;
}
add_filter( 'bp_notifications_get_registered_components', 'custom_filter_notifications_get_registered_components' );

// this hooks to post creation and saves the post id
function bp_custom_add_notification( $post_id, $post ) {
    if ( $post->post_type == 'project' || $post->post_type == 'bid' ) {
        $post = get_post( $post_id );
        $author_id = $post->post_author;
        bp_notifications_add_notification( array(
            'user_id'           => $author_id,
            'item_id'           => $post_id,
            'component_name'    => 'projectadd',
            'component_action'  => 'projectadd_action',
            'date_notified'     => bp_core_current_time(),
            'is_new'            => 1,
        ) );
    }   
}
add_action( 'wp_insert_post', 'bp_custom_add_notification', 99, 2 );

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

        $post = get_post( $item_id );

        $custom_title = $post->post_author . ' add the project ' . get_the_title( $item_id );
        $custom_link  = get_permalink( $post );
        $custom_text = $post->post_author . ' add the project ' . get_the_title( $item_id );
        // WordPress Toolbar
        if ( 'string' === $format ) {
            $return = apply_filters( 'projectadd_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( 'projectadd_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, 7 ); 

Solution

  • You've changed the number and order of arguments from the example you linked. The two arguments you added are never used. Restore the arguments in the function to match the example and the the number in your add_filter to 5.

    function custom_format_buddypress_notifications( $content, $item_id, $secondary_item_id, $total_items, $format = 'string', $action, $component ) 
    {
    ...
    }
    add_filter( 'bp_notifications_get_notifications_for_user', 'custom_format_buddypress_notifications', 10, 7 );
    

    needs to be

    function custom_format_buddypress_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) 
    {
    ...
    }
    add_filter( 'bp_notifications_get_notifications_for_user', 'custom_format_buddypress_notifications', 10, 5 );