Skip to:
Content
Pages
Categories
Search
Top
Bottom

CPT in Activity with Detail


  • Ben Riga
    Participant

    @benbuzztoniccom

    I have figured out how to get my Custom Post Types into the activity stream and it all works well so I’d like to refine that somewhat and am not sure where to start. I’d like to include a lot more detail about the CPT in the activity.

    I noticed that when I turn on the ‘Site Tracking’ component and then create a new post I get the standard ‘John wrote a new post, <post title>’ but it also includes the first 200 or so chars from the post content. I’d like to do something like that but build a custom string from the data that is stored in the CPT.

    So end up with something like this:
    <<<
    JohnSmith checked in
    John drank ABC Beer at XYZ Bar and rated it:5.
    Note: The beer was great. I loved it
    <<<

    I’d love any pointers on how I might be able to do something like that. Or if you could point me to the code that the Site Tracking component uses to insert Posts into the activity stream that might be a good start also.

    Thanks,
    Ben

Viewing 11 replies - 1 through 11 (of 11 total)

  • shanebp
    Moderator

    @shanebp

    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 );

    Ben Riga
    Participant

    @benbuzztoniccom

    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


    shanebp
    Moderator

    @shanebp

    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


    Ben Riga
    Participant

    @benbuzztoniccom

    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.


    shanebp
    Moderator

    @shanebp

    …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.


    Ben Riga
    Participant

    @benbuzztoniccom

    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


    Ben Riga
    Participant

    @benbuzztoniccom

    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


    Ben Riga
    Participant

    @benbuzztoniccom

    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


    Ben Riga
    Participant

    @benbuzztoniccom

    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


    dianaascher
    Participant

    @dianaascher

    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!


    Ben Riga
    Participant

    @benbuzztoniccom

    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 );
Viewing 11 replies - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.
Skip to toolbar