Need some php help - Custom Post Types and Buddypress

I've been working to get custom post types working in the BuddyPress activity stream. I was working with another member in this thread: http://premium.wpmudev.org/forums/topic/how-to-add-custom-posts-types-in-buddypress-activity

However, it turns out that the background for the code in that thread is over a year old and BP 1.5 uses a far more elegant solution to adding custom post types to the activity stream http://bpdevel.wordpress.com/2011/08/12/custom-post-types-comments-and-the-activity-stream-in-bp-1-5/

As such, I'm working with wikis and products (from the Wiki plugin and MarketPress)

To get the Wikis to record to activity I created this and added it to bp-custom.php:

// add wiki post types to the activity stream
function activity_publish_wiki_posts($wikiposts) {
   $wikiposts[] = 'incsub_wiki';
   return $wikiposts;
}
add_filter ( 'bp_blogs_record_post_post_types', 'activity_publish_wiki_posts' );

For Products, I have this:

// add product post types to the activity stream
function activity_publish_product_posts($productposts) {
   $productposts[] = 'product';
   return $productposts;
}
add_filter ( 'bp_blogs_record_post_post_types', 'activity_publish_product_posts' );

Instead of using two separate snippets you can also streamline the code by putting both post types into the array, like this:

// add product post types to the activity stream
function activity_publish_custom_post_types($cpts) {
   $cpts[] = 'product';
   $cpts[] = 'incsub_wiki';
   return $cpts;
}
add_filter ( 'bp_blogs_record_post_post_types', 'activity_publish_custom_post_types' );

These are very efficient ways to add custom post types to the activity stream. I tested this several times and it works great. It also ensures that when a post is updated, the activity record gets updated. I see this as essential behavior, especially with the Wikis.

Help Needed
Now, I separated them into two different snippets because I need to get some customization built into them. I want the activity record to read "John updated a wiki article, Cool Stuff, on the site, My Wiki" for the Wikis, and "John added a new product, My Cool Car, to the site, John's Cars"

I've gone through various bits of code and tried adding a filter to the snippets I post above that would filter $activity_action from BuddyPress core in bp-blogs-function.php. I just can't seem to get that in there and working properly.

Can someone help me modify those snippets to get the correct line of text in the activity update?

Bonus snippet:
Here's a bonus for anyone bothering to read through my wall o' text:
In BP 1.5 comments to custom post types no longer make it into the activity stream. Fix that with some code like this, just change the names of your post types in the array!

// add custom post type comments to the activity stream
function activity_publish_post_type_comments( $types ) {
   $types[] = 'incsub_wiki';
   $types[] = 'product';
   return $types;
}
add_filter( 'bp_blogs_record_comment_post_types', 'activity_publish_post_type_comments' );

Note: according to the BP devs, this method is the beginning of an API they intend to build into future versions. They intend to not degrade it or remove it, but build on it so if you use it in a plugin or in bp-custom.php - it will still work fine in future versions.

    Saunt Valerian

    Yeah, the linked to but of code at pastebin is the one I'm already aware of but is over a year old now and it predates BP 1.5 quite a bit. It basically works, but it also fails in some ways (it fails to update and bump the activity record when a post is updated).

    Anyhow, I did get some information about it though and am trying to modify the function with the code below, but I'm getting a syntax error regarding the semi-colon at the end of the bp_blogs_record_activity array - what am I doing wrong? Can someone help me clean this up?

    // add wiki post types to the activity stream
    function activity_publish_wiki_posts($wikiposts) {
       $wikiposts[] = 'incsub_wiki';
       return $wikiposts;
    
       bp_blogs_record_activity( array(
       	'type'   => 'new_blog_post',
       	'action' => apply_filters( 'bp_blogs_activity_new_post_action', sprintf( __( '%1$s updated the article, %2$s, in the %3$s', 'buddypress' ), bp_core_get_userlink( (int)$post->post_author ), '<a href="' . $post_permalink . '">' . $post->post_title . '</a>', '<a href="' . get_blog_option( $blog_id, 'home' ) . '">' . get_blog_option( $blog_id, 'blogname' ) . '</a>' )
         ) ) ;
    
    }
    add_filter ( 'bp_blogs_record_post_post_types', 'activity_publish_wiki_posts' );

    Also, once the array is setup into the function, is there some command (return bp_blogs_record_activity, for example) that needs to be add below it?

    Saunt Valerian

    Here's another try.

    I tried loading the type and action parameters into the initial array, but that didn't work. I didn't get any errors, but it broke things and failed to add to the activity stream.

    This is what I tried:

    // add wiki post types to the activity stream
    function activity_publish_wiki_posts($wikiposts) {
       $wikiposts[] = array(
       		'incsub_wiki',
       		'type' => 'new_blog_post',
       		'action' => apply_filters( 'bp_blogs_activity_new_post_action', sprintf( __( '%1$s updated the article, %2$s, in the %3$s', 'buddypress' ), bp_core_get_userlink( (int)$post->post_author ), '<a href="' . $post_permalink . '">' . $post->post_title . '</a>', '<a href="' . get_blog_option( $blog_id, 'home' ) . '">' . get_blog_option( $blog_id, 'blogname' ) . '</a>' ))
       		);
    
    return $wikiposts;
    
    }
    add_filter ( 'bp_blogs_record_post_post_types', 'activity_publish_wiki_posts' );

    Still looking for help on this.

    tracysurf
    • Elite
    • 8
    • #12,075

    Thanks for your code. That helped me. Now I hope I can help you.

    In bp-custom.php put:

    /Change wording in Activity Stream and use post_type iso of 'post'
    add_filter( 'bp_blogs_activity_new_post_action', 'my_activity_content',  2, 10 );
    
    function my_activity_content($activity_content, $post, $post_permalink){
    
            $activity_content = sprintf( __( '%s submitted a new '.$post->post_type.': %s', 'buddypress' ), bp_core_get_userlink( (int)$post->post_author ), '<a href="' . $post_permalink . '">' . $post->post_title . '</a>' );
            $activity_content .= bp_create_excerpt( $post->post_content, 25 );
    
            return $activity_content;
    }

Join 368618 happy members

Get access to our entire collection of epic plugins, astonishing themes & genius support

Become a member