BuddyDev

Search

[Resolved] Error on "Show the faces of users who liked a BuddyPress Activity"

  • Participant
    Level: Initiated
    Posts: 19
    Alayna on #3412

    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!

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #3413

    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
    Ravi

  • Participant
    Level: Initiated
    Posts: 19
    Alayna on #3417

    Thanks Ravi! Works like a charm.

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #3418

    Hi Alayna

    Thank You for confirming. I am glad that i could help.

    Thank You
    Ravi

  • Participant
    Level: Initiated
    Posts: 19
    Alayna on #3841

    Hello again!

    After further testing I realized that this code only displays the avatar of the first user who liked an activity, not everyone who liked it. Is there a fix for this?

  • Participant
    Level: Initiated
    Posts: 19
    Alayna on #3851

    Hi, disregard my earlier post, I’ve found an alternate solutions. Thanks!

  • Participant
    Level: Initiated
    Posts: 8
    Stefan on #51632

    Hi 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.

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #51633

    Hello Stefan,

    With BuddyBoss you do not need this as recently introduced reactions features. Please take a look at that.

    Regards
    Ravi

You must be logged in to reply to this topic.

This topic is: resolved