0

I am using beneath code im buddypress to show notification of the latest posts posted by users.

https://gist.github.com/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

1

Like 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 );
  • 1
    I would suggest using 'bp_core_get_users' where you can filter users based on buddypress function. Yours will work to. But using ' bp_core_get_users ' will give more flexibility – JItendra Rana Apr 18 '17 at 12:58
  • Sure. You're right. – ahendwh2 Apr 18 '17 at 13:01
  • Hi is it also possible to pull notifications upon [EventOn] (myeventon.com) Plugin new event submission. – Ahmad Apr 19 '17 at 8:34
  • I don't know the plugin, but you have to search for the action which is triggered, when a new event is created and then hook the creation of a notification to this action. According to this it seems, that the plugin uses the standard WordPress publishing, so you can use the action publish_post (like above) and check for the post type: if ( $post->post_type == 'xx') – ahendwh2 Apr 19 '17 at 9:40
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.

  • i want to send notification to all user id – Ahmad Apr 18 '17 at 12:42
  • Updated the answer. Please check. – JItendra Rana Apr 18 '17 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.