问题
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.
I have seen the documentation here: 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:
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
回答1:
You should attach your function with 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 );
Where add_action
holds:
- 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
来源:https://stackoverflow.com/questions/29313820/wordpress-i-cannot-call-bp-notifications-add-notification