How to show Favorite Counter for each activity in Buddypress Activity Page

How to show Favorite Counter for each activity in Buddypress Activity Page

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

ছোট্টমনে প্রশ্ন জাগে

পাশের বাসার পিচ্চিঃ “আপু, জানো? আমার আম্মুর বেবি হবে। আমার বোন লাগবে।” বললাম, “কেন ভাই হলে ভালো হয় না?” সে বলল, “ওর জন্য তো আমি ভাই আছি-ই। আম্মুর তাহলে ছেলে মেয়ে সব থা…

Source: ছোট্টমনে প্রশ্ন জাগে

Adding Attachments to PDF Form

This is a upgraded post of adding attachment to a PDF form from eslifeline.wordpress.com. The PDF file at that link has some error/ less functionality. This file includes some more functionality.

functionalities:

  • This file filter files like .exe | .bat | .cmd | .zip | .jar | .rar | .msi | .dat.
  • This file loads all the attachments name in the list box.
  • This file counts attached file perfectly.
  • This file shows warning on attaching filtered files.
  • This file shows warning on successful attachment.

For the file click here.

N.B: THIS WILL ONLY WORK WHEN YOU OPEN THE FORM WITH ACROBAT.IF YOU WANT IT TO WORK IN READER YOU WILL NEED TO APPLY USAGE RIGHTS USING THE READER EXTENSIONS ON THE SERVER.