BuddyPress Create Notification

Trying to get my head around BuddyPress notifications.

If I wanted to send a notification to myself (user ID) 5 during a running function how would I go about doing it?

https://codex.buddypress.org/developer/function-examples/bp_notifications_add_notification/

  • Adam Czajczyk

    Hello Kasey,

    I hope you're having a nice day!

    On the page that you linked to there's an example of how to send a notification and it's taken from BP activity module. I assume that you'd like to send a notification but instead of hooking it to the "bp_activity_sent_mention_email" you would like to call it from "inside" of some of your custom function. Do I understand that correctly?

    Let's assume that you got this function (in "pseudo-code"):

    function my_function() {
    
    echo "Hello!";
    
    // send_BP_notification_here;
    
    echo "Notification sent!";
    }

    Now in place where the "send_BP_notification_here" is you would like to trigger your own BP notification. To do this, you will want to introduce a custom action hook there and execute all associated functions. Example:

    function my_function() {
    
    echo "Hello!";
    
    do_action('send_my_function_notifications');
    
    echo "Notification sent!";
    }

    Then you would only need to add a code similar to the one from example on BP docs page and just hook it to "send_my_function_notifications" hook:

    function bp_my_function_notifications() {
    
    if (bp_is_active('notifications')) {
    bp_notifications_add_notifications(array(
    // here you set your notification parameters
    ));
    }
    }
    add_action('send_my_function_notifications',''bp_my_function_notifications');

    Now when your function ("my_function()" function) is executed it first prints "Hello!" then executes all functions assigned to the "send_my_function_notifications" hook - so it executes the "bp_my_function_notifications" function and then prints "Notification sent!".

    I hope that makes sense :slight_smile: Would that help or did I missed anything? Let me know please if you need any more/other assistance with this.

    Best regards,
    Adam

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.