wordpress - 我无法调用 bp_notifications_add_notification [英] wordpress - I cannot call bp_notifications_add_notification
问题描述
我是 wordpress 开发的新手,我想开始开发一个我将在我的网站中使用的个人插件.我的网站使用 wordpress 和 buddypress.在buddypress中,他们有通知,这很好.但我希望我的插件也向 buddypress 添加通知,并且会向会员显示.
I am new in wordpress development and I want to start to develop a personal plugin which will i use in my site. my site uses wordpress and buddypress. In buddypress, they have notifications, which is pretty good. but i want my plugin to also add notifications to buddypress as well, and will appear to members.
我在这里看到了文档:bp_notifications_add_notification()
到目前为止我的代码如下.请注意,我删除了插件的某些部分只是为了简化它
so far my code is below. Please note that i have removed may parts of the plugin just to simplify it
<?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
?>
但是当我运行它时.它说:
but when ever I run it. it says:
致命错误:调用 C:\xampp\htdocs\htbcph\wp-content\plugins\test\test-plugin.php 中未定义的函数 bp_notifications_add_notification()在线14
Fatal error: Call to undefined function bp_notifications_add_notification() in C:\xampp\htdocs\htbcph\wp-content\plugins\test\test-plugin.php on line 14
我认为问题是,我需要先包含组件.但我会怎么做呢?请为我提供对我有帮助的好教程的链接.谢谢
i think the problem is, i need to include the component first. but how will i do it? please provide me links for good tutorials that will help me. Thanks
推荐答案
你应该用 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
所在的位置:
- bp_activity_sent_mention_email 是一个预定义的钩子/动作,
- sample_add_notification 您自己定义的将使用钩子调用的函数
- 10 优先
- 1 传递的参数个数,你只传递了
$u_id
所以它是 1
- bp_activity_sent_mention_email is a predefined hook/action,
- sample_add_notification your own defined function that will call with hook
- 10 Priority
- 1 Number of argument passed, you have passed only
$u_id
so it is 1
这篇关于wordpress - 我无法调用 bp_notifications_add_notification的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!