wordpress - WordPress 在点击时将 BuddyPress 自定义通知标记为已读
问题描述
我在 Buddypress 中创建了一个自定义通知。每当开始一个新主题时,它就会被发送以供审核,并且所有版主都会收到通知和电子邮件以批准该帖子。通知看起来像这样(单击它会转到主持人可以批准/拒绝帖子的主题)-
当我点击通知时,它没有被标记为已读。我了解通知链接应该有一个“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。谁能指出我正确的方向。提前致谢。
解决方案
我实际上是为活动邀请(自定义帖子类型)制作的,在我的情况下,我可以从不同用户那里获得关于同一活动的多个邀请,但它也适用于你的情况。
我们将使用的函数:
bp_notifications_mark_notifications_by_item_id();
它可以在bp-notifications-functions.php或buddyboss 函数参考中找到。
首先,您需要准备网址。
$args = array(
'action' => 'custom_action',
'item_id' => $item_id,
'secondary_id' => $secondary_item_id
);
// Add the args to the URL.
$custom_link = add_query_arg( $args, get_the_permalink($item_id) );
“行动”可以是任何东西。
你不要在这里使用随机数,因为它只持续 24 小时,如果用户在那之后检查通知,你的删除它的功能将不起作用。
第二个函数将处理通知的取消标记:
function my_post_mark_read() {
if (get_post_type() === 'my_post_slug') {
if (isset($_GET['item_id']) && isset($_GET['secondary_id']) && isset($_GET['action']) && $_GET['action'] === 'custom_action') {
$user_id = get_current_user_id();
$item_id = htmlentities($_GET['item_id'], ENT_QUOTES, 'UTF-8');
$component_name = 'my_component_name';
$component_action = 'my_component_action';
$secondary_item_id = htmlentities($_GET['secondary_id']);
$is_new = 0;
bp_notifications_mark_notifications_by_item_id( $user_id, $item_id, $component_name, $component_action, $secondary_item_id, $is_new );
}
}
}
add_action('wp', 'my_post_mark_read');
您不需要$post
为此功能获取对象。您可以通过 id 获取帖子作者姓名$secondary_item_id
,如下所示:
$author_name = get_user_by('id', $secondary_item_id)->display_name;
bp_core_get_user_displayname( $post->post_author )
但是您从不使用它,而是bp_core_get_user_displayname( $secondary_item_id )
使用$post
. 你的$custom_title
和$custom_text
是一样的。您应该只使用一个变量而不是重复自己。
推荐阅读
- java - Checkstyle ConstantName 规则适用于并非真正恒定的事物
- scheme - Scheme语言中是否有正常的“设置”功能(不是特殊形式)?
- java - Java 字符串中 + 和 += 的区别
- mysql - 为 MySql 创建多个表比创建单个表更快
- typescript - 类型和继承
- clickhouse - 使用 rpad 函数远程 mySQL 不起作用
- javascript - 发送数据和用户命令以运行 Puppeteer 脚本
- corda - Corda - FlowMonitor - id X 的流已经等待 Y 秒来接收来自各方的消息
- python - Deep animator 无法正确安装
- python - 使用 requests.get 获取数据时 URL 中的特殊字符