How To Display Favorite Count after each activity in Buddypress Group

BuddyPress  is a powerful social network plugin for WordPress with member profiles, user groups, activity Streams, messaging, topics and many more. Members inside BuddyPress can socially interact with each other with blogging platform environment. Today, over 50,000+ live sites are running with the BuddyPress Plugin. Despite powerful features, BuddyPress lacks few features. One of them is favorite count.

Although BuddyPress Group has a Favorite option for each activity, the visitors cannot see how many users have favorited the activity. However, we can display favorite count using a simple trick.  So , in this article, I will show you how to display Favorite Count after each activity in Buddypress Group without page refresh or reload. Just follow the following steps precisely.


Display Favorite Count after each activity in Buddypress Group


Inside functions.php file of your active theme, add the following code at the very bottom.

function display_favorite_count() {
  $my_fav_count = bp_activity_get_meta( bp_get_activity_id( ), ' favorite_count ' );
   if( !empty( $my_fav_count ) ):?>
    <a name="favorite-<?php bp_activity_id();?>" class=" button acomment-reply bp-primary-action ">Favorited<span><?php printf( _n( ' once', '%s times', $my_fav_count ), $my_fav_count );?></span></a>
   <?php endif;
 }
add_action( 'bp_activity_entry_meta', 'display_favorite_count' );

Here’s The Screenshot

Favorite Count BuddyPress

Everyone has a buddy who laughs funnier than he jokes. Click To Tweet

If you want to Display Favorite Counts similar to Comment Counts Span, then you’ll need to edit the BuddyPress entry.php file from the editor.Let’s say, you may want to display Favorite Count activity like the screenshot below.

Display Favorite Count after each activity in Buddypress

You need to replace some codes from BuddyPress activity template File. Inside BuddyPress plugin, search for entry.php file, which is located inside buddypress\bp-themes\bp-default\activity. Copy that whole activity folder inside your Theme File and Change Some Line Of Codes as below.

Note: Do Not Change core Plugin code.

REPLACE this code which you can find inside activity/entry.php file, opening via the editor.

<?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" title="<?php esc_attr_e( 'Mark as Favorite', 'buddypress' ); ?>"><?php _e( 'Favorite', 'buddypress' ); ?></a>
<?php else : ?>
<a href="<?php bp_activity_unfavorite_link(); ?>" class="button unfav bp-secondary-action" title="<?php esc_attr_e( 'Remove Favorite', 'buddypress' ); ?>"><?php _e( 'Remove Favorite', 'buddypress' ); ?></a>
<?php endif; ?>
<?php endif; ?>

WITH THIS CODE

<?php if ( bp_activity_can_favorite() ) : ?>
<?php $my_fav_count = bp_activity_get_meta( bp_get_activity_id(), 'favorite_count' ); ?>
<?php if ( !bp_get_activity_is_favorite() ) : ?>
<a href="<?php bp_activity_favorite_link(); ?>" class="button fav bp-secondary-action" title="<?php esc_attr_e( 'Mark as Favorite', 'buddypress' ); ?>"><span>Favorite <?php if( !empty( $my_fav_count ) ): ?><span><?php echo $my_fav_count; ?></span><?php endif; ?></a>

<?php else : ?>
<a href="<?php bp_activity_unfavorite_link(); ?>" class="button unfav bp-secondary-action" title="<?php esc_attr_e( 'Remove Favorite', 'buddypress' ); ?>"><span>Remove Favorite <span><?php echo $my_fav_count; ?></span></a>
<?php endif; ?>
<?php endif; ?>

or You can Download entry.php directly from here and replace with your existing file.

ALSO READ  BuddyPress Group Custom MetaBox in edit group page

Download entry.php code file 

You’re Not Done Yet. Now we need to add some stylesheets in our custom.css file. Copy the following code in your theme’s custom CSS file. This is necessary for the standard layout of your favorite count span button.

#buddypress a.bp-secondary-action span span {
 background: #999;
 color: #fff;
 font-size: 90%;
 margin-left: 2px;
 padding: 0 5px;
}

Now, refresh your activity Page. You’ll See The Favorite Comment Count in your BuddyPress Group Activity if any user has already marked it as a favorite. If you hit the favorite button, you will notice the favorite count gets increased or vice versa.  Let me know if it worked. Happy BuddyPress buddy !! 😀