wordpress - I cannot call bp_notifications_add_notification

时光怂恿深爱的人放手 提交于 2019-12-25 05:30:53

问题


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:

  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


来源:https://stackoverflow.com/questions/29313820/wordpress-i-cannot-call-bp-notifications-add-notification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!