BuddyPress, powerful social network plugin for WordPress having member profiles, user groups, user activity, messaging, adding user as friend, forums, topics and many more. There is a “Favorite” button available for each activity in activity page. After clicking on it, that counts how many users mark the activity as favorite. But that not show after Favorite button like Comment button does. So, now how to show that there without page refresh/reload again?

Here we go…

BuddyPress has it’s own theme system for WordPress too. That available under buddypress\bp-themes\bp-default\. If we inject our code/requirement there, when BuddyPress will update, that will be erased. So, we don’t do anything there. As we just need to show favorite counter in activity page after Favorite button, we will need buddypress\bp-themes\bp-default\activity directory just. So, just copy this directory to your WordPress theme directory. Then remove all files but entry.php from the directory. Asking to delete all the files, because, if you used any custom css/styling for activity page that may be override and your activity page may look like odd.

So, now as you have activity directory and entry.php inside it in your WordPress theme directory, just open that in editor. Then, remove following lines of code:

<?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 following:

<?php if ( bp_activity_can_favorite() ) : ?>
<?php
$favorite_count = bp_activity_get_meta( bp_get_activity_id(), 'favorite_count' );
$favorite_count = (!empty($favorite_count)) ? $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' ); ?>">Favorite <?=$favorite_count;?></a>
<?php else : ?>
<a href="<?php bp_activity_unfavorite_link(); ?>" class="button unfav bp-secondary-action" title="<?php esc_attr_e( 'Remove Favorite', 'buddypress' ); ?>">Remove Favorite <?=$favorite_count;?></a>
<?php endif; ?>
<?php endif; ?>

Then, refresh your activity page. If any activity is already marked as favorite by any user, you will see, total number after Favorite button. Now, if you click on Favorite button, you will see that, number will disappeared. But we need latest total value of favorite. Right?

So, now again. How!?

Now, open your WordPress theme’s functions.php script and add following code at the bottom.

add_action( 'wp_ajax_activity_mark_fav', 'override_bp_legacy_theme_mark_activity_favorite' );
add_action( 'wp_ajax_nopriv_activity_mark_fav', 'override_bp_legacy_theme_mark_activity_favorite' );

add_action( 'wp_ajax_activity_mark_unfav', 'override_bp_legacy_theme_unmark_activity_favorite' );
add_action( 'wp_ajax_nopriv_activity_mark_unfav', 'override_bp_legacy_theme_unmark_activity_favorite' );

/**
* Inject favorite count for activity on click on Favorite
**/
function override_bp_legacy_theme_mark_activity_favorite() {
// Bail if not a POST action.
if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
return;

if ( bp_activity_add_user_favorite( $_POST['id'] ) ){
$fav_count = bp_activity_get_meta( $_POST['id'], 'favorite_count' );
_e( "Remove Favorite {$fav_count}", 'buddypress' );
}else{
$fav_count = bp_activity_get_meta( $_POST['id'], 'favorite_count' );
_e( "Favorite {$fav_count}", 'buddypress' );
}

exit;
}

/**
* Inject favorite count for activity on click on Un-favourite
**/
function override_bp_legacy_theme_unmark_activity_favorite() {
// Bail if not a POST action.
if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
return;

if ( bp_activity_remove_user_favorite( $_POST['id'] ) ){
$fav_count = bp_activity_get_meta( $_POST['id'], 'favorite_count' );
_e( "Favorite {$fav_count}", 'buddypress' );
}else{
$fav_count = bp_activity_get_meta( $_POST['id'], 'favorite_count' );
_e( "Remove Favorite {$fav_count}", 'buddypress' );
}

exit;
}

Then, save the script, and refresh activity page and check. That’s it 🙂

Happy BuddyPress in WordPress 😉

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s