在 post 中创建自定义通知插入 buddypress

Create custom notification in post insert in buddypress

我想在 post 创建时添加自定义通知。我已经关注了这个 url https://webdevstudios.com/2015/10/06/buddypress-adding-custom-notifications/

我所做的是,我在创建项目或出价 post 类型时添加了自定义通知。但它不起作用。请检查我的代码,告诉我我做错了什么。

// 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 ); 

您已更改链接示例中参数的数量和顺序。您添加的两个参数从未使用过。恢复函数中的参数以匹配示例和 add_filter 中的数字到 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 );

需要

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 );