It sounds like you want to filter the content body of the activity item.
There is a filter hook for that:
apply_filters_ref_array( 'bp_get_activity_content_body', array( $activities_template->activity->content, &$activities_template->activity ) );
in function bp_activity_content_body()
in
buddypress\bp-activity\bp-activity-template.php
I think the main issue will be determining if the entry is related to your CPT.
And that will be affected by how you created the CPT.
There may be some info in the second argument – maybe in ["type"]
?
Try:
function ben_filter_activity_body( $content, $activity ) {
var_dump( $activity );
$content = ' Start of your content ' . $content;
return $content;
}
add_filter( 'bp_get_activity_content_body', 'ben_filter_activity_body', 25, 2 );
Thanks, Shane,
This is helpful. It does seem to work for all types except my CPTs. 🙁 For my Check In CPT it does not appear to fire at all (i.e. I’m not seeing the var_dump or the ‘start of content’ message).
One thing I should have mentioned is that I am using the Pods plugin to define my CPTs. So that is probably why the filter does not called, I guess. Would you have any idea why that might be? Any suggestions for what I might try to get around that?
Thanks for the help and support,
Ben
I’m not familiar with Pods.
…it does not appear to fire at all
Then it’s not being filtered when the activity loop is being output.
Check the database activity table for an entry for one of your CPTs.
Make a small edit and see if it appears.
If so, the database entry is being used.
Therefore you may need to filter the entry when it is created.
Take a look at the filters in function save
in the class BP_Activity_Activity
or maybe
function bp_activity_create_summary
Pods = http://pods.io/
Pods has been useful to help me prototype and prove that I can build what I want to build but probably won’t scale to where I need it so I think I’ll look to pull that and build the CPTs in a more traditional way. Let me do that and then come back to this once I have it working again.
Thanks for the help and support.
…build the CPTs in a more traditional way.
That would be useful knowledge, but registering CPTs for BP can be a bit different.
If you haven’t, look at this codex page.
And you may actually want to build a custom BP component.
Yup. Thanks. I already had my CPT working in BP using the methods described in that codex page. It was posting to the Activity stream. What was not working was having the ability to customize what information showed up in the stream. I think some of that was due to my using Pods to dynamically generate the CPTs (and of course some of it could be due to my lack of knowledge).
At any rate, I had hit some limits in Pods anyway so it’s time to learn how to manually code those. It doesn’t look too complicated (famous last words :)) so I think it should just take me a couple of days.
Thanks again,
Ben
Actually the BP component idea might be a good one for me also. I spent some time trying to find the source for the Site Tracking component as I thought I might be able to learn a lot from that but I was never able to find it. Would you know where I would look for that?
Thanks,
Ben
Hi Shane,
This did not take me much time at all. I was able to define the CPT in a plugin and everything works as expected.
The one thing I’m not sure how to fix though is getting more detail out of my CPT. I have some meta box fields defined that contain info I’d like to show in the activity stream. I’m not seeing those in the var_dump so I’m not sure how I would extract those.
Would you have an idea about that?
Thanks,
Ben
I’m working on other parts of the app for now but still need to solve the problem of how to get a lot more detail into the stream. Has anyone done that before?
Any help or pointers on how to pull info out of custom meta box fields and insert that into the activity stream would be greatly appreciated.
Thanks,
Ben
I have been trying to do this with fields from several different sources, with mixed success. I’d be very interested in how you end up doing this!
Sure. I finally got back to this and spent some time debugging it. I had assumed that the meta box fields would be available but in retrospect that was not a good assumption (why would they, right?). Once I figured that out I just wrote some code to retrieve the fields I needed. If you use $activity->secondary_item_id you can pull in the data you need from the original meta box fields.
The final code from my tests is below.
I hope this helps. And, thanks to Shane for putting me on the right path.
function ben_filter_activity_body( $content, $activity ) {
$checkintext = "";
if ( 'new_check_in' === $activity->type ) {
if ( isset( $activity->secondary_item_id ) ) {
$current_post = $activity->secondary_item_id;
$beerName = get_post_meta($current_post, 'check_in_details_beer-name', true);
$barName = get_post_meta($current_post, 'check_in_details_bar-name', true);
$beerRating = get_post_meta($current_post, 'check_in_details_rating', true);
$beerReview = get_post_meta($current_post, 'check_in_details_notes', true);
$checkintext = "Drank: " . $beerName . " At: " . $barName . "<br />Rated it: " . $beerRating . "<br />Review: " . $beerReview;
}
$content = 'Note: (ID:'. $activity->secondary_item_id . ')' . $content . $checkintext;
}
return $content;
}
add_filter( 'bp_get_activity_content_body', 'ben_filter_activity_body', 25, 2 );