notifications - 在buddypress中添加自定义通知

标签 notifications buddypress

我想在发生特定事件时向我的 buddypress“通知”选项卡添加自定义通知。如何实现这一目标?

最佳答案

我遵循了本教程并且非常完整。 为我工作

BuddyPress: Adding Custom Notifications



我打算把作者写的。但是如果你直接去那个帖子会更好,在那里你可以找到更好的解释。我觉得这个帖子是给傻瓜写的,很完整,解释的很清楚,竟然还有 gist .

第一注册您的组件

您需要将您的通知注册为 budypress 组件。这很容易。注册的组件名称是 定制
// 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, 'custom' );
    // Return component's with 'custom' appended
    return $component_names;
}
add_filter( 'bp_notifications_get_registered_components', 'custom_filter_notifications_get_registered_components' );

2nd 渲染通知
// this gets the saved item id, compiles some data and then displays the notification
function custom_format_buddypress_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) {
    // New custom notifications
    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 ) {
            $return = apply_filters( 'custom_filter', '<a href="' . esc_url( $custom_link ) . '" title="' . esc_attr( $custom_title ) . '">' . esc_html( $custom_text ) . '</a>', $custom_text, $custom_link );
        // 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', 'custom_format_buddypress_notifications', 10, 5 );

第 3 次启动通知

Here you add the notification when someone writes you on a post. Use the action hook wp_insert_comment for catch that event.


// this hooks to comment creation and saves the comment id
function bp_custom_add_notification( $comment_id, $comment_object ) {
    $post = get_post( $comment_object->comment_post_ID );
    $author_id = $post->post_author;
    bp_notifications_add_notification( array(
        'user_id'           => $author_id,
        'item_id'           => $comment_id,
        'component_name'    => 'custom',
        'component_action'  => 'custom_action',
        'date_notified'     => bp_core_current_time(),
        'is_new'            => 1,
    ) );

}
add_action( 'wp_insert_comment', 'bp_custom_add_notification', 99, 2 );

全部一起
<?php
// 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, 'custom' );

    // Return component's with 'custom' appended
    return $component_names;
}
add_filter( 'bp_notifications_get_registered_components', 'custom_filter_notifications_get_registered_components' );


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

    // New custom notifications
    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 ) {
            $return = apply_filters( '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 {
            $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', 'custom_format_buddypress_notifications', 10, 5 );


// this hooks to comment creation and saves the comment id
function bp_custom_add_notification( $comment_id, $comment_object ) {

    $post = get_post( $comment_object->comment_post_ID );
    $author_id = $post->post_author;

    bp_notifications_add_notification( array(
        'user_id'           => $author_id,
        'item_id'           => $comment_id,
        'component_name'    => 'custom',
        'component_action'  => 'custom_action',
        'date_notified'     => bp_core_current_time(),
        'is_new'            => 1,
    ) );

}
add_action( 'wp_insert_comment', 'bp_custom_add_notification', 99, 2 );

关于notifications - 在buddypress中添加自定义通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23148948/

相关文章:

android - 通过点击通知判断app是否启动的方法

javascript - 解决 WordPress 子主题上的 javascript 与父主题冲突

iphone - MPMediaLibrary DidChangeNotification 调用了两次?

ios - 如何在前台收到通知时弹出消息框?

android - 在没有 C2DM 的情况下通过本地网络将通知从设备推送到设备

php - mysql 如何合并具有相同 id 的多行

php - 如何在buddypress中mysql查询 `$wpdb->get_var` `bp_groups`?

css - 如何更改 BuddyBoss 主题中链接的 CSS

wordpress - Buddypress 链接不起作用

iOS:手机锁定时本地通知显示速度非常慢