Hi there. I used the activity hack given here https://buddydev.com/snippets/662-show-the-faces-of-users-who-liked-a/ to try and display the profile pictures of those that favorited an activity but I got this error instead:
Fatal error: Cannot redeclare bp_activity_add_meta() (previously declared in /Applications/MAMP/htdocs/wordpresstest/wp-content/plugins/bp-custom.php:5) in /Applications/MAMP/htdocs/wordpresstest/wp-content/plugins/buddypress/bp-activity/bp-activity-functions.php on line 1233
Any idea on how to fix this? Thanks in advance!
Hi Alayna,
Welcome to BuddyDev.The code snippet is old and from the time when bp_activity_add_meta was not part of BuddyPress. You most probably put that code in bp-custom.php and that’s why you are getting the error. if it was put in functions.php, It would not have generated the error.
Please remove the cold code and use the following code in your bp-custom.php
function bpdev_track_user_favorite( $activity_id, $user_id ) { bp_activity_add_meta( $activity_id, 'favorited_by_user', $user_id ); } add_action( 'bp_activity_add_user_favorite', 'bpdev_track_user_favorite', 10, 2 ); function bpdev_track_user_unfavorite( $activity_id, $user_id ){ bp_activity_delete_meta( $activity_id, 'favorited_by_user', $user_id ); } add_action( 'bp_activity_remove_user_favorite', 'bpdev_track_user_unfavorite', 10, 2 ); //show faces, yay! function bpdev_show_who_favorited_activities() { $output = ''; $favorited_users = bp_activity_get_meta( bp_get_activity_id(), 'favorited_by_user' ); //print_r($favorited_users); if ( ! empty( $favorited_users ) ) { foreach ( (array) $favorited_users as $user_id ) { $output .= "<a href='" . bp_core_get_user_domain ( $user_id ) . "'>". bp_core_fetch_avatar ( array( 'type'=> 'thumb', 'height'=> 25, 'width'=>25, 'item_id'=> $user_id ) ) . "</a>"; } } if ( $output ) { echo "<div class='clearfix activity-favorited-by'>{$output}</div>"; } } add_action( 'bp_activity_entry_content', 'bpdev_show_who_favorited_activities' );
After putting this code try favoriting an activity and reloading the page to see it works or not. It should work.
Please do let me know if it is working for you or not?
Thank you
RaviHi Ravi,
I just tried to use your above-mentioned code:function bpdev_track_user_favorite( $activity_id, $user_id ) {
bp_activity_add_meta( $activity_id, ‘favorited_by_user’, $user_id );
}
add_action( ‘bp_activity_add_user_favorite’, ‘bpdev_track_user_favorite’, 10, 2 );function bpdev_track_user_unfavorite( $activity_id, $user_id ){
bp_activity_delete_meta( $activity_id, ‘favorited_by_user’, $user_id );
}
add_action( ‘bp_activity_remove_user_favorite’, ‘bpdev_track_user_unfavorite’, 10, 2 );
//show faces, yay!function bpdev_show_who_favorited_activities() {
$output = ”;
$favorited_users = bp_activity_get_meta( bp_get_activity_id(), ‘favorited_by_user’ );
//print_r($favorited_users);
if ( ! empty( $favorited_users ) ) {foreach ( (array) $favorited_users as $user_id ) {
$output .= ““. bp_core_fetch_avatar ( array( ‘type’=> ‘thumb’, ‘height’=> 25, ‘width’=>25, ‘item_id’=> $user_id ) ) . ““;
}}
if ( $output ) {
echo “<div class=’clearfix activity-favorited-by’>{$output}</div>”;
}}
add_action( ‘bp_activity_entry_content’, ‘bpdev_show_who_favorited_activities’ );It works but it only shows the first user who liked an activity post.
Would be great to show a specific number of users who liked a post.
Do I need to use a different hook or action name (after all the past updates)?
By the way, I am using BuddyBoss.
You must be logged in to reply to this topic.