How to Add BuddyPress Activity Stream Content to a Related Blog Post

Let’s say you’ve got BuddyPress setup on your site, and you want to add any activity stream content to the related blog post. For example, if you post a new blog post to your site, it’ll show up something like this in the activity stream:

screenshot showing an activity stream entry in Buddypress sharing a new blog post, and an associated comment
While this is a highly customized version of the BuddyPress theme, you can see that this is the automatic entry to the BuddyPress stream that is created after a new post is published.

Note that I’ve replaced the standard “Favorite” text with the term “Like”, and will use the terminology “like” instead of “favorite”, but otherwise it’s all just default functionality.

Now what if you wanted to show just that activity stream entry on the actual blog post page itself. You could use it to replace comments altogether, and to make more of a “Facebook” experience, where users can like the blog post, not just the associated activity stream entry. Note that technically, they’re still liking the activity stream entry, but to anyone on the frontend, it all appears unified.

Here’s how to do it.

First, open up your single.php file in your theme. It should look something like this, though no doubt yours will have additional elements as well:

<?php get_header(); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<h1 class="entry-title"><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php comments_template( '', true ); ?>
<?php endwhile; ?>
<?php get_footer(); ?>

That’s about as simple as a single.php file could be. In yours, just find the call to comments_template(); and we’ll just go ahead and completely replace that. You don’t have to replace it, you could allow both normal WordPress comments as well as BuddyPress replies, but normal WP comments don’t show up in the activity feed and having two “commenting” system seems like a bad user experience to me. So just delete that entire line, and replace it with the following:

<aside class="this-post-activity">
<?php do_action( 'bp_before_directory_activity' ); ?>
<div id="buddypress">
<?php do_action( 'bp_before_directory_activity_content' ); ?>
<?php do_action( 'template_notices' ); ?>
<?php do_action( 'bp_before_directory_activity_list' ); ?>
<div class="activity">
<?php do_action( 'bp_before_activity_loop' ); ?>
<?php $thisblogpost = $post->ID; ?>
<?php if ( bp_has_activities( bp_ajax_querystring( 'activity' ) . '&secondary_id='. $thisblogpost ) ) : ?>
<?php if ( empty( $_POST['page'] ) ) : ?>
<ul id="activity-stream" class="activity-list item-list">
<?php endif; ?>
<?php while ( bp_activities() ) : bp_the_activity(); ?>
<?php do_action( 'bp_before_activity_entry' ); ?> <li class="<?php bp_activity_css_class(); ?>" id="activity-<?php bp_activity_id(); ?>">
<div class="activity-content">
<div class="activity-meta">
<?php if ( bp_get_activity_type() == 'activity_comment' ) : ?>
<a href="<?php bp_activity_thread_permalink(); ?>" class="button view bp-secondary-action" title="<?php esc_attr_e( 'View Conversation', 'buddypress' ); ?>"><?php _e( 'View Conversation', 'buddypress' ); ?></a>
<?php endif; ?>
<?php if ( is_user_logged_in() ) : ?>
<?php if ( bp_activity_can_favorite() ) : ?>
<?php if ( !bp_get_activity_is_favorite() ) : ?>
<a href="<?php bp_activity_favorite_link(); ?>" class="button fav bp-secondary-action button-tertiary" title="<?php esc_attr_e( 'Like This', 'buddypress' ); ?>"><?php _e( 'Like', 'buddypress' ); ?></a>
<?php else : ?>
<a href="<?php bp_activity_unfavorite_link(); ?>" class="button unfav bp-secondary-action button-primary" title="<?php esc_attr_e( 'Unlike', 'buddypress' ); ?>"><?php _e( 'Unlike', 'buddypress' ); ?></a>
<?php endif; ?>
<?php endif; ?>
<?php if ( bp_activity_can_comment() ) : ?>
<a href="<?php bp_activity_comment_link(); ?>" class="button acomment-reply bp-primary-action button-secondary" id="acomment-comment-<?php bp_activity_id(); ?>"><?php printf( __( 'Reply', 'buddypress' ), bp_activity_get_comment_count() ); ?></a>
<?php endif; ?>
<?php endif; ?>
</div>
</div>
<?php do_action( 'bp_before_activity_entry_comments' ); ?>
<?php if ( ( bp_activity_get_comment_count() || bp_activity_can_comment() ) || bp_is_single_activity() ) : ?>
<div class="activity-comments">
<?php bp_activity_comments(); ?>
<?php if ( is_user_logged_in() && bp_activity_can_comment() ) : ?>
<form action="<?php bp_activity_comment_form_action(); ?>" method="post" id="ac-form-<?php bp_activity_id(); ?>" class="ac-form"<?php bp_activity_comment_form_nojs_display(); ?>>
<div class="ac-reply-avatar"><?php bp_loggedin_user_avatar('width=50&height=50'); ?></div>
<div class="ac-reply-content">
<div class="ac-textarea">
<textarea id="ac-input-<?php bp_activity_id(); ?>" class="ac-input bp-suggestions" name="ac_input_<?php bp_activity_id(); ?>"></textarea>
</div>
<input type="submit" name="ac_form_submit" value="<?php esc_attr_e( 'Post', 'buddypress' ); ?>" />   <a href="#" class="ac-reply-cancel"><?php _e( 'Cancel', 'buddypress' ); ?></a>
<input type="hidden" name="comment_form_id" value="<?php bp_activity_id(); ?>" />
</div>
<?php do_action( 'bp_activity_entry_comments' ); ?>
<?php wp_nonce_field( 'new_activity_comment', '_wpnonce_new_activity_comment' ); ?>
</form>
<?php endif; ?>
</div>
<?php endif; ?>
<?php do_action( 'bp_after_activity_entry_comments' ); ?>
</li>
<?php do_action( 'bp_after_activity_entry' ); ?>
<?php endwhile; ?>
<?php if ( bp_activity_has_more_items() ) : ?>
<li class="load-more">
<a href="<?php bp_activity_load_more_link() ?>"><?php _e( 'Load More', 'buddypress' ); ?></a>
</li>
<?php endif; ?>
<?php if ( empty( $_POST['page'] ) ) : ?>
</ul>
<?php endif; ?>
<?php else : ?>
<div id="message" class="info">
<p><?php _e( 'Sorry, there was no activity found. Please try a different filter.', 'buddypress' ); ?></p>
</div>
<?php endif; ?>
<?php do_action( 'bp_after_activity_loop' ); ?>
<?php if ( empty( $_POST['page'] ) ) : ?>
<form action="" name="activity-loop-form" id="activity-loop-form" method="post">
<?php wp_nonce_field( 'activity_filter', '_wpnonce_activity_filter' ); ?>
</form>
<?php endif; ?>
</div>
<?php do_action( 'bp_after_directory_activity_list' ); ?>
<?php do_action( 'bp_directory_activity_content' ); ?>
<?php do_action( 'bp_after_directory_activity_content' ); ?>
<?php do_action( 'bp_after_directory_activity' ); ?> </div>
</aside>

Whoa! That’s a lot of code!

Okay so admittedly I’m still getting used to how BuddyPress works and also on a tight deadline (so why am I taking the time to write this post?!) Anyway, what I’ve basically done is added the code from the /community/activity/activity-loop.php file and then inside of that also the /community/activity/entry.php. I didn’t just include those files because I wanted to use the $post object from the single.php file (ie, the current blog post) and didn’t have time to figure out how to pass that into those files. Suggestions are welcome.

There are also a few modifications to those files.

For example, I’m not showing the status update box, or the actual activity feed entry itself (ie, the part that shows the author’s avatar, the “Nathan wrote a new post, Post Title, 10 hours ago”, or the excerpt that usually accompanies it.

I’m only showing associated comments and the Like and Reply buttons.

So where’s the magic? Well, if you look for this line of code:

<?php $thisblogpost = $post->ID; ?>

That’s it! We’re assigning the ID of the WordPress Post itself to the $thisblogpost variable.

Then when we go to do our Activity Loop, we add that on as the secondary_id parameter, like so:

<?php if ( bp_has_activities( bp_ajax_querystring( 'activity' ) . '&secondary_id='. $thisblogpost ) ) : ?>

You can find more info on the BuddyPress Activity Loop and how that works here.

So we’re essentially just showing a BuddyPress activity loop and restricting it only to replies associated with this blog post.

Voila! That’s all, feel free to style it to your heart’s content! Here’s the final output in my setup:

screenshot showing activity stream replies, favorite and reply button on a blog post
From here, you can Like/Favorite, and Reply just as you could from the normal BP Activity Stream.

I’ve been working with WordPress for something like 15 years now, and aside from custom stuff for my clients, have never made a plugin. I’m thinking this functionality is incredibly useful for just about any BuddyPress site, though, so if I end up getting a ton of +1s on this post, this will be my first official WordPress / BuddyPress plugin. And I’ll make it available completely for free.

So if you dig this, go for that +1 button up there near the top of the page and maybe we can get everyone’s BP installs running a little more like Facebook-users (and other social media consumers) would expect.

Up Next: Remove Padding / Margin from WordPress Admin Bar