wordpress - I cannot call bp_notifications_add_not

2019-09-09 16:44发布

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条回答
forever°为你锁心
2楼-- · 2019-09-09 17:25

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:

  1. bp_activity_sent_mention_email is a predefined hook/action,
  2. sample_add_notification your own defined function that will call with hook
  3. 10 Priority
  4. 1 Number of argument passed, you have passed only $u_id so it is 1
查看更多
登录 后发表回答