如何修改BuddyPress自定义通知链接实现点击后标记为已读
问题:BuddyPress自定义通知点击后无法自动标记为已读如何处理
我在BuddyPress中创建了自定义通知,每当有新Topic发布时,内容会进入审核流程,所有版主都会收到通知和邮件,提醒其审核帖子。点击通知会跳转到对应话题页面,版主可在该页面执行批准或驳回操作,通知样式如下:
但我点击通知时,该通知不会被自动标记为已读。我了解到通知链接需要包含bbp_mark_read动作才能实现标记已读效果,但我找不到将该动作添加到通知URL的正确方法,我的代码如下:
// Display notification. add_filter( 'bp_notifications_get_notifications_for_user', 'bbms_moderation_alert_notifications', 10, 5 ); function bbms_moderation_alert_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) { // New custom notifications if ( 'bbms_moderation_alert_action' === $action ) { $post = get_post( $item_id ); //$item_id represents ID of the Topic $author_name = get_the_author_meta('display_name', $post->post_author); $custom_title = '[Moderate] ' .bp_core_get_user_displayname( $post->post_author ) . ' created a Topic "' . get_the_title( $item_id ) . '"'; $custom_link = get_permalink( $post ); //Need to create a wp_nonce_url with bbp_mark_topic action here $custom_text = '[Moderate] ' .bp_core_get_user_displayname( $post->post_author ) . ' created a Topic "' . get_the_title( $item_id ) . '"'; // WordPress Toolbar if ( 'string' === $format ) { $return = apply_filters( 'bbms_moderation_alert_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( 'bbms_moderation_alert_filter', array( 'text' => $custom_text, 'link' => $custom_link ), $custom_link, (int) $total_items, $custom_text, $custom_title ); } return $return; } return $action; }
我需要修改上述代码中的$custom_link变量,请问该如何操作?
解决方案
直接替换原有$custom_link相关代码即可,修改后的完整代码如下:
// Display notification. add_filter( 'bp_notifications_get_notifications_for_user', 'bbms_moderation_alert_notifications', 10, 5 ); function bbms_moderation_alert_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) { // New custom notifications if ( 'bbms_moderation_alert_action' === $action ) { $post = get_post( $item_id ); //$item_id represents ID of the Topic $author_name = get_the_author_meta('display_name', $post->post_author); $custom_title = '[Moderate] ' .bp_core_get_user_displayname( $post->post_author ) . ' created a Topic "' . get_the_title( $item_id ) . '"'; // 修改开始 $base_permalink = get_permalink( $post ); $custom_link = wp_nonce_url( add_query_arg( array( 'action' => 'bbp_mark_read', 'topic_id' => $item_id ), $base_permalink ), 'bbp_mark_topic_' . $item_id ); // 修改结束 $custom_text = '[Moderate] ' .bp_core_get_user_displayname( $post->post_author ) . ' created a Topic "' . get_the_title( $item_id ) . '"'; // WordPress Toolbar if ( 'string' === $format ) { $return = apply_filters( 'bbms_moderation_alert_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( 'bbms_moderation_alert_filter', array( 'text' => $custom_text, 'link' => $custom_link ), $custom_link, (int) $total_items, $custom_text, $custom_title ); } return $return; } return $action; }
修改说明
- 用
add_query_arg方法在原话题永久链接上追加bbp_mark_read动作参数和对应话题ID参数,匹配bbPress原生的标记已读规则 - 用
wp_nonce_url生成带安全校验的链接,nonce规则bbp_mark_topic_{$item_id}完全匹配bbPress默认的校验逻辑,无需额外添加其他钩子即可生效 - 版主点击通知后,bbPress会自动识别链接参数,校验通过后先将对应通知标记为已读,再跳转至话题详情页,不影响原有审核操作流程
内容的提问来源于stack exchange,提问作者Puneet Saini
相关产品推荐
相关产品推荐

