0
votes

The blog-post(wp-post) was not appearing in the activity stream out of the box in my installation of buddypress. So i added a piece of code in bp-acitivity-actions.php: function buddypress_edit_blog_post($post_id) { global $bp, $user_id; $post = get_post($post_id); $title = $post->post_title; $user_fullname = bp_core_get_user_displayname($user_id);

 bp_activity_add(array(
'action' => $user_fullname.' updated ' . $title . ':',
'component' => 'blog_post',
'type' => 'update_post',
'primary_link' => get_permalink($post_id),
'user_id' => $user_id,
'content' => $post->post_content
 ));
}
add_action('edit_post', 'buddypress_edit_blog_post');

and now the site wide activity is displaying user’s new blog-post, however, I still can’t see it in personal activity stream. I don’t understand, why does this happen? And I want to know which piece of code is managing the personal activity stream, I would like to know how it works.

And after i replaced the global $user_id with a local variable $user_id which is returned by wp_get_current_user(), nothing changed. Here is the new version of my code:

function buddypress_edit_blog_post($post_id) {
 global $bp; //, $user_id;
 $user_id = wp_get_current_user();
 $post = get_post($post_id); 
 $title = $post->post_title;
 $user_fullname  = bp_core_get_user_displayname($user_id);

 bp_activity_add(array(
'action' => $user_fullname.' updated ' . $title . ':',
'component' => 'blog_post',
'type' => 'update_post',
'primary_link' => get_permalink($post_id),
'user_id' => $user_id,
'content' => $post->post_content
 ));

}
add_action('edit_post', 'buddypress_edit_blog_post');

Thanks.

2

2 Answers

0
votes

The variable $user_id inside your bp_activity_add() function will need to be set to a user's ID in order for the activity item to show up in that user's personal stream. Be sure the value is set properly.

Now, getting the user's ID is slightly different depending on the particular user you need. For example:

  • bp_loggedin_user_id() will get you the ID of the user currently logged in
  • bp_displayed_user_id() returns the ID of the currently displayed user

You should use the function appropriate to your use case.

0
votes

wp_get_current_user() returns an object, not a single variable. https://codex.wordpress.org/Function_Reference/wp_get_current_user

Or you can use bp_loggedin_user_id() as henrywright says.

So i added a piece of code in bp-acitivity-actions.php

You should not change core files. Your function can go in your theme/functions.php.