Need urgent help with BP notifications
-
Thanks in advance for who may help me.
I´ve been trying, for more than one week, to create a custom notification and always got the same error, so I really need help:
WordPress database error: [Table 'dbase_wp.n' doesn't exist] SELECT * FROM n WHERE user_id IN (2) AND secondary_item_id IN (393) AND component_name IN ('activity') AND component_action IN ('new_at_mention') AND is_new = 1 WordPress database error: [Incorrect table name ''] INSERT INTO <code></code> (<code>user_id</code>, <code>item_id</code>,<code>secondary_item_id</code>, <code>component_name</code>, <code>component_action</code>,<code>date_notified</code>, <code>is_new</code>) VALUES (2,0,393,'activity','new_at_mention','',1) The code Im using to try this is: function buddypress_celebrities_add_notification ( $activity, $subject, $message, $content, $receiver_user_id ) { global $wpdb; $bp = buddypress(); $subject = 'Celebrity birthday'; $message = '1 Say happy birthday to $famoso_name'; $content = '2 Say happy birthday to $famoso_name'; $args = array( 'user_id' => 2, 'item_id' => $activity->id, 'secondary_item_id' => 393, 'component_name' => buddypress()->activity->id, 'component_action' => 'new_at_mention', 'date_notified' => $current_time, 'is_new' => 1, ); bp_notifications_add_notification( $args ); // $prueba = $args; } add_action( 'bp_activity_sent_mention_email', 'buddypress_celebrities_add_notification', 10, 1 ); // Then I call the function buddypress_celebrities_add_notification ( );
//and after it DATABASE ERROR…
If someone can explain me what is going on or just point me in the righ direction to fix and understand this, I’ll be forever grateful.
-
Ok, now I am trying this:
//function class function setup_globals( $args = array() ) { global $bp; $sr_notifications_test_slug = 'sr_notifications_test'; parent::setup_globals( array( 'id' => 'sr_notifications_test', 'slug' => $sr_notifications_test_slug, 'notification_callback' => array( $this, 'sr_format_notifications_test' ) ) ); /* Register this in the active components array */ $bp->active_components[$sr_notifications_test_slug] = 'sr_notifications_test'; // do_action( 'setup_globals' ); } //format notofication function sr_format_notifications_test( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) { switch ( $action ) { case 'my_test': $link = get_permalink( $item_id ); $text = 'Test Notification'; $return = apply_filters( $filter, array( 'text' => $text, 'link' => $link ), $link, (int) $total_items, $text, $link, $item_id, $secondary_item_id ); break; } do_action( 'sr_format_notifications_test', $action, $item_id, $secondary_item_id, $total_items ); return $return; } //Notification added to DB: bp_notifications_add_notification( array( 'user_id' => $user_id, 'item_id' => $activity->id, 'secondary_item_id' => $activity->user_id, 'component_name' => buddypress()->activity->id, 'component_action' => 'my_test', 'date_notified' => bp_core_current_time(), 'is_new' => 1 ) ); print_r ($bp->active_components[$sr_notifications_test_slug]);
and this is the error I have: (still the same)
WordPress database error: [Table 'WP_db_funs.n' doesn't exist] SELECT * FROM n WHERE component_name IN ('activity') AND component_action IN ('my_test') AND is_new = 1 WordPress database error: [Incorrect table name ”] INSERT INTO (user_id,item_id,secondary_item_id,component_name,component_action,date_notified,is_new) VALUES (0,0,0,'activity','my_test','2015-08-12 15:57:21′,1)
Please if anybody Knows about this I will apreciate any help 🙁
Table ‘WP_db_funs.n’ doesn’t exist how come?? table n ??
Please use the code button when you publish code !
First, you can’t use a function name twice. (setup_globals)
Second, it’s more easier to do after reading the correct documentation:And to find some answers once you have this information:
http://stackoverflow.com/questions/23148948/add-a-custom-notification-in-buddypressThe dev discussion around the new notification system:
https://buddypress.trac.wordpress.org/ticket/5148And the appropriate filter
bp_notifications_get_notifications_for_user
which let non BP component hook into notification:
https://buddypress.trac.wordpress.org/changeset/7578Thanks for reply.
Yes I read those articles (…and much more), but still not understanding, Sorry about it, thus I am asking for help.
I don,t understand what do you mean with use a function name twice:
(// do_action( ‘setup_globals’ ); This is commented) ,is this a better way to do it?:
function sr_notifications_test_setup_globals () { global $bp, $current_blog; $bp->sr_notifications_test = new stdClass(); $bp->sr_notifications_test->id = 'sr_notifications_test'; $bp->sr_notifications_test->slug = 'sr_notifications_test'; $bp->sr_notifications_test->notification_callback = 'sr_format_notifications_test'; $bp->active_components[$bp->sr_notifications_test->id] = $bp->sr_notifications_test->id; do_action( 'sr_notifications_test_setup_globals ' ); } add_action( 'bp_notification_settings', 'sr_notifications_test_setup_globals ' );
Anyway, what I understood is that I have to write those 3 functions to make my custom notifications works:
1.- setup_globals
2.- format_notifications (called from set_up_globals, notification_callback)
If Iunderstood you´, the filter I have to use is bp_notifications_get_notifications_for_user
3.- _add_notificationis this the right way?
I mean what you wrote in your first example:
function setup_globals( $args = array() ) {...}
The right way is the one indicated on codex and by @henrywright on stackexchange. (in fact, the same snippet).
The question now is, what do you want to notify to a user ? You’re testing, ok, but what’s your purpose ? We need an activity ! For example, is celebrities (in your first topic) a custom post type ?
The example on Codex tells a user he was mentionned. This is now built in BP, so it’s no more representative of how to insert a non core activity. It shows you only the way to do it, and you can’t use it by simply copy/pasting.
Thanks @danbp ,
I´ll try to explain you:I have a custom_post_type (celebrities). it real name is “famosos”.
BuddyPressUsers can mark any celebrity as fasvourite.
This part is working fine.
So now, I am trying to send a notification (to users that have marked as favorite certain celebriry) that “today” is that celebrity´s birthday.For that purpose, I have a SQL query that works fine and gives me an array with budypress user id and the celebritie post id. (this is also working fine)
SO I have to setup a custom notificationa that checks every day that SQL QUery to gets users and celebrities ids to notify user about today´s birthday (of their favourite celebrities)
and this is what I did at first:
define( 'BP_ACTIVITY_CELBIRTHDAY_SLUG', 'celbirthday_notifier' ); function celbirthday_notifier_setup_globals () { $bp = buddypress(); $bp->celbirthday_notifier = new stdClass(); $bp->celbirthday_notifier->id = 'celbirthday_notifier'; //I asume others are not going to use this is $bp->celbirthday_notifier->slug = BP_ACTIVITY_CELBIRTHDAY_SLUG; $bp->celbirthday_notifier->notification_callback = 'celbirthday_notifier_format_notifications'; //show the notification $bp->active_components[$bp->celbirthday_notifier->id] = $bp->celbirthday_notifier->id; do_action( 'celbirthday_notifier_setup_globals' ); } add_action( 'bp_setup_globals', 'celbirthday_notifier_setup_globals' );
and then try to format the notification:
function celbirthday_notifier_format_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) { if ( 'new_birthday' === $action ) { $famoso_name = get_the_title( $secondary_item_id ); $subject = 'Celebrity birthday'; $message = 'Say happy birthday to $famoso_name'; $content = 'Tody is $famoso_name birthday .....somethin else '; if ( (int) $total_items > 1 ) { $text = sprintf( __( 'You have %d celebrities birthday', 'bbpress' ), (int) $total_items ); $filter = 'bp_activity_at_message_notification_message'; } else { if ( !empty( $secondary_item_id ) ) { //nada } else { $text = sprintf( __( 'You have %d new birthday of %s', 'bbpress' ), (int) $total_items, $famoso_name ); } $filter = 'bp_activity_at_message_notification_message'; } } if ( 'string' === $format ) { //falta definir $famoso_link y mas $return = apply_filters( $filter, '<a href="' . esc_url( $famoso_link ) . '" title="birthday anounce">' . esc_html( $text ) . '</a>', (int) $total_items, $text, $famoso_link ); // $message = apply_filters( 'bp_activity_at_message_notification_message', $message, $poster_name, $content, $message_link, $settings_link ); } } do_action( 'celbirthday_notifier_format_notifications', $action, $item_id, $secondary_item_id, $total_items );
and then add notification this way:
function buddypress_celebrities_add_notification( $fecha_act, $famoso_name, $receiver_user_id) { if ( bp_is_active( 'notifications' ) ) { $fecha_act = date("Y-m-d"); $res_birth = $wpdb->get_results( "SELECT wp_best_favs.user_id, wp_postmeta.post_id FROM wp_best_favs LEFT JOIN wp_postmeta ON ( wp_postmeta.post_id = wp_best_favs.celeb_ID ) WHERE ( wp_postmeta.meta_key = 'fecha_na') AND (DAY( <code>wp_postmeta</code>.<code>meta_value</code> ) = DAY( '$fecha_act' )) AND (MONTH( <code>wp_postmeta</code>.<code>meta_value</code> ) = MONTH( '$fecha_act' ) ) ") ; $res_birth = array_filter($res_birth); $matriz = objectToArray($res_birth); //convertimos el array de objetos en array normal para manejarlo mas facil $current_time = bp_core_current_time(); if(count($matriz)==0) { // está vacío } else { global $wpdb; $bp = buddypress(); $subject = 'Celebrity birthday'; $message = '1 Say happy birthday to $famoso_name'; $content = '2 Say happy birthday to $famoso_name'; foreach ($matriz as $v1) { $famoso_id = $v1[post_id]; $famoso_name = get_the_title ($v1[post_id]); $receiver_user_id = $v1[user_id]; $args = array( 'user_id' => $receiver_user_id, 'item_id' => $famoso_id, 'secondary_item_id' => $famoso_id, 'component_name' => $bp->celbirthday_notifier->id, 'component_action' => 'new_birthday', 'date_notified' => $current_time, 'is_new' => 1, ); bp_notifications_add_notification( $args ); } } } } add_action( 'new_birthday', 'buddypress_celebrities_add_notification', 10, 1 );
so far this is what I am trying to do, hope you understand.
Thanks in advance.Let’s ask @shanebp, @henrywright, i’m not a dev ninja.
Thank you @danbp , will see, in the main time I keep trying and will update if I make progress.
okay, This is almost done, but is really rare, not everything works as it should.
First of all, this is not a pluggin, I am writing all these code in bp-functions.php in my bududdypress folder within my theme folder. (this may be my problem).
Step by step:
1st Problem setup globals is not working as it espected:define( 'BP_NOTIFIER_CELBIRTHDAY_SLUG', 'celbirthday_notifier' ); function celbirthday_notifier_setup_globals () { global $bp, $wpdb; $bp = buddypress(); $bp->celbirthday_notifier = new stdClass(); $bp->celbirthday_notifier->id = 'celbirthday_notifier'; //I asume others are not going to use this is $bp->celbirthday_notifier->slug = BP_NOTIFIER_CELBIRTHDAY_SLUG; $bp->celbirthday_notifier->notification_callback = 'celbirthday_notifier_format_notifications'; //show the notification $bp->active_components[$bp->celbirthday_notifier->id] = $bp->celbirthday_notifier->id; do_action( 'bp_setup_globals' ); }
This function is not doing nothing unless I call it directly:
celbirthday_notifier_setup_globals ();
I Know this is not the proper way, but it is the only way I found to make it run.
Does anyone know why it does not work properly?Continue with the notification:
//Format notifications function celbirthday_notifier_format_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) { if ( 'new_birthday' === $action ) { if ( (int) $total_items > 1 ) { if ( 'string' === $format ) { $famoso_name = get_the_title( $secondary_item_id ); return apply_filters( 'celebrities_multiple_verifications_notification','Say Happybirthday to '.$famoso_name); } } else { if ( 'string' === $format ) { //falta definir $famoso_link y mas $famoso_name = get_the_title( $secondary_item_id ); return apply_filters( 'celebrities_single_verifications_notification','Say Happybirthday to '.$famoso_name); // $return = apply_filters( $filter, '<a href="' . esc_url( $famoso_link ) . '" title="birthday anounce">' . esc_html( $text ) . '</a>', (int) $total_items, $text, $famoso_link ); // $message = apply_filters( 'bp_activity_at_message_notification_message', $message, $poster_name, $content, $message_link, $settings_link ); } } } do_action( 'celbirthday_notifier_format_notifications', $action, $item_id, $secondary_item_id, $total_items ); return false; }
ok, this format function is working right, although I have to do some more work. After it, I have two more functions (mark and delete notifications) wich are also working fine.
//mark notification function celebrities_mark_screen_notifications() { global $bp; bp_notifications_mark_notifications_by_item_id( $bp->loggedin_user->id, $bp->celbirthday_notifier->id, 'new_birthday', $secondary_item_id = false, $is_new='0'); } add_action( 'bp_notifications_screen', 'celebrities_mark_screen_notifications' );
and…
function celbirthday_notifier_remove_screen_notifications() { global $bp; bp_core_delete_notifications_for_user_by_type( $bp->loggedin_user->id, $bp->profile->slug, 'new_birthday' ); } add_action( 'bp_notifications_screen', 'celbirthday_notifier_remove_screen_notifications' ); add_action( 'xprofile_screen_display_profile', 'celbirthday_notifier_remove_screen_notifications' );
As I say, this works fine, but a I am not sure if I need the second “add_action” (xprofile screen).
And finally, to add notification I use this code (which is not working)://Adding notification function buddypress_celebrities_add_notification() { if ( bp_is_active( 'notifications' ) ) { $fecha_act = date("Y-m-d"); $res_birth = $wpdb->get_results( "SELECT wp_best_favs.user_id, wp_postmeta.post_id FROM wp_best_favs LEFT JOIN wp_postmeta ON ( wp_postmeta.post_id = wp_best_favs.celeb_ID ) WHERE ( wp_postmeta.meta_key = 'fecha_na') AND (DAY( <code>wp_postmeta</code>.<code>meta_value</code> ) = DAY( '$fecha_act' )) AND (MONTH( <code>wp_postmeta</code>.<code>meta_value</code> ) = MONTH( '$fecha_act' )) "); $res_birth = array_filter($res_birth); $matriz = objectToArray($res_birth); //I use this function to convert objet array to simple array if(count($matriz)==0) { // está vacío } else { global $wpdb; $bp = buddypress(); $current_time = bp_core_current_time(); foreach ($matriz as $v1) { $famoso_id = $v1[post_id]; $receiver_user_id = $v1[user_id]; $args = array( 'user_id' => $receiver_user_id, 'item_id' => $famoso_id, 'secondary_item_id' => $famoso_id, 'component_name' => $bp->celbirthday_notifier->slug, 'component_action' => 'new_birthday', 'date_notified' => $current_time, 'is_new' => 1, ); bp_notifications_add_notification( $args ); } } } } do_action( 'bp_init', 'buddypress_celebrities_add_notification' );
So this is my bigger problem:
Add notifications (function buddypress_celebrities_add_notification()) is not working.
I am pretty sure this is because I am not hooking properly, but I tried so many hooks and none of it did work.
I Know the code is fine because if I comment the two first lines and the two last lines (so I run the code not as a function) the notifications are being added to database.Anyone can help me, to find a proper hook, or just pointing me in the right direction.
Thanks in advance.I am writing all these code in bp-functions.php in my bududdypress folder within my theme folder. (this may be my problem).
Yeah, it is probably the problem. Custom functions for BP can/should be added to bp-custom.php, which should sit in wp-content/plugins/bp-custom.php, not in your child theme buddypress folder.
Or into functions.php of your child-theme.
bp-custom.php explained on Codex.
thanks @danbp ,
I just made test, but still the same. I put the first function (celbirthday_notifier_setup_globals ()) in bp-custom.php within plugins folder and still the same, the function does nothing, unless you called:
celbirthday_notifier_setup_globals ();
I believe that I am missing some basics to understand this.
- The topic ‘Need urgent help with BP notifications’ is closed to new replies.