在buddypress

时间:2018-04-24 07:25:22

标签: wordpress buddypress

我想在创建帖子时添加自定义通知。我已关注此网址https://webdevstudios.com/2015/10/06/buddypress-adding-custom-notifications/

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

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

0 个答案:

没有答案