wordpress - 我无法调用 bp_notifications_add_notification

wordpress - I cannot call bp_notifications_add_notification

我是 wordpress 开发的新手,我想开始开发一个我将在我的网站中使用的个人插件。我的网站使用 wordpress 和 buddypress。在 buddypress 中,他们有通知,这非常好。但我希望我的插件也能向 buddypress 添加通知,并且会向会员显示。

我在这里看到了文档: bp_notifications_add_notification()

到目前为止,我的代码如下。请注意,我已经删除了插件的可能部分只是为了简化它

<?php
/*
Plugin Name: Test
Description: personal plugin for my site
Version:     1.0.0
*/

function sample_add_notification( $u_id ) {

    $args = array(
        'user_id' => $u_id
    );

    bp_notifications_add_notification( $args );
}

sample_add_notification( 2 ); //this line should write a new notification for user_id: 2

?>

但我什么时候运行它。它说:

致命错误:调用 C:\xampp\htdocs\htbcph\wp-content\plugins\test\test-plugin.php[= 中的未定义函数 bp_notifications_add_notification() 28=] 在线 14

我认为问题是,我需要先包含该组件。但我该怎么做呢? 请为我提供对我有帮助的好教程的链接。谢谢

您应该使用 hook/action

附加您的函数
function sample_add_notification( $u_id ) {

    $args = array(
        'user_id' => $u_id
    );

    // Make sure the noticications has been activated
    if ( bp_is_active( 'notifications' ) ) {
        bp_notifications_add_notification( $args );
    }
}
add_action( 'bp_activity_sent_mention_email', 'sample_add_notification', 10, 1 );

其中 add_action 成立:

  1. bp_activity_sent_mention_email 是预定义的 hook/action,
  2. sample_add_notification 您自己定义的函数,将使用钩子调用
  3. 10 优先级
  4. 1 传递的参数个数,你只传递了 $u_id 所以是 1