I am using beneath code im buddypress to show notification of the latest posts posted by users.
The problem is that when a post is posted then only the person who posts the post receive the notification and the other users don't receive.
I want to fix this error.
Thanks and regards, Ahmad
I am using beneath code im buddypress to show notification of the latest posts posted by users.
https://gist.github/kishoresahoo/9209b9f6ac69ce21cd6962e7fe21e1b4/6b9d901383da4783f9108ffdc6aa2bb4e671cd44
The problem is that when a post is posted then only the person who posts the post receive the notification and the other users don't receive.
I want to fix this error.
Thanks and regards, Ahmad
Share Improve this question edited Apr 18, 2017 at 10:34 fuxia♦ 107k39 gold badges255 silver badges459 bronze badges asked Apr 18, 2017 at 9:52 TeslaTesla 33 bronze badges2 Answers
Reset to default 1Like JItendra Rana already mentioned, you just send a notification to the author. If all users should get a notification, you must loop through all your users and add a notification to everyone. You get the users with get_users. Here is the modified code to realize it:
function bp_post_published_notification( $post_id, $post ) {
$author_id = $post->post_author; /* Post author ID. */
$users = get_users();
/* Loop all users */
foreach( $users as $user ) {
if ( bp_is_active( 'notifications' ) ) {
bp_notifications_add_notification( array(
'user_id' => $user->ID,
'item_id' => $post_id,
'component_name' => 'custom',
'component_action' => 'custom_action',
'date_notified' => bp_core_current_time(),
'is_new' => 1,
) );
}
}
}
add_action( 'publish_post', 'bp_post_published_notification', 99, 2 );
If you check the function for 'publish_post' action.
function bp_post_published_notification( $post_id, $post ) {
$author_id = $post->post_author; /* Post author ID. */
if ( bp_is_active( 'notifications' ) ) {
bp_notifications_add_notification( array(
'user_id' => $author_id,
'item_id' => $post_id,
'component_name' => 'custom',
'component_action' => 'custom_action',
'date_notified' => bp_core_current_time(),
'is_new' => 1,
) );
}
}
add_action( 'publish_post', 'bp_post_published_notification', 99, 2 );
Notice that you are sending Notification to Post Author only. You will need to change 'user_id' parameter in 'bp_notifications_add_notification' function to the user id you want.
Here is the Codex Documentation for bp_notifications_add_notification Function.
EDIT :
Update the function as below :
function bp_post_published_notification( $post_id, $post ) {
// $author_id = $post->post_author;
$active_users = bp_core_get_users( array("per_page"=>-1));
foreach ( $active_users['users'] as $user ) {
if ( bp_is_active( 'notifications' ) ) {
bp_notifications_add_notification( array(
'user_id' => $user->ID,
'item_id' => $post_id,
'component_name' => 'custom',
'component_action' => 'custom_action',
'date_notified' => bp_core_current_time(),
'is_new' => 1,
) );
}
}
}
add_action( 'publish_post', 'bp_post_published_notification', 99, 2 );
Click here for more details about 'bp_core_get_users' function.