In Buddypress activity steam, Buddypress provides you a Favorite button which is similar to Facebook ‘Like’. When any user click on Favorite button, an activity filter tab opened named ‘favorites’ which contain all favorite activities of that user.

Now Whats about Activity’s perspective? I mean, how can I show user names just below these activity meta button (Comment, Favorite etc)? there no default way, but lets write a function or may be plugin for it. First we need to get users who Favorited a particular activity.

When user’s favorite activities information are saved as an array in wp_usermeta table, which meta key is ‘bp_favorite_activities’
So we can get user ID from this table if I know activity ID, we can get activity ID simply calling bp_get_activity_id() inside the loop, and as bp_favorite_activities saved as an array in database, we can user LIKE command with activity id to know if that particular activity id exist on that array or not..yes, we could also do that with php array matching function, but it is faster I think.

so.. the query will be,

 

[codesyntax lang=”php”]
SELECT user_id FROM wp_usermeta WHERE meta_key = ‘bp_favorite_activities’ AND meta_value LIKE ‘%:”$activity_id”;%
[/codesyntax]

Now, we got user Id’s using that query. we want to show user’s display name after activity who liked that before. so we need
user’s display name; we can use this BP function to get user’s display name

[codesyntax lang=”php”]

bp_get_profile_field_data(

array(
‘field’ => 1,
‘user_id’ => $user_id
));

 

[/codesyntax]

 

we also want to link up this names to user’s profile page too, so, we need user’s profile page’s link. we can use this..

[codesyntax lang=”php”]

$link = bp_core_get_user_domain($user_id);

[/codesyntax]

The whole function can be written as:

 

 

[codesyntax lang=”php”]

function get_users_fav($activity_id='')
{
$activity_id = bp_get_activity_id();
global $wpdb;
$query = "SELECT user_id FROM ".$wpdb->base_prefix."usermeta WHERE meta_key = 'bp_favorite_activities' AND meta_value LIKE '%:\"$activity_id\";%' ";
$users = $wpdb->get_results($query,ARRAY_A);
foreach ($users as $user)
{
$name = bp_get_profile_field_data(
array(
'field' => 1,
'user_id' => $user['user_id']
));
$link = bp_core_get_user_domain($user['user_id']);
$u_names[++$i] = '<a class="activity_fav_users" href="'.$link.'"'.">$name</a>";
}
if(count($u_names))
echo '<div class="fav_box">Favorite of: '.implode(',', $u_names).'</div>';
else
return '';
}

[/codesyntax]

 

Now, need to hook up with BP filter so that the result will be shown at the end of each activity.

add_filter( ‘bp_activity_entry_meta’, ‘get_users_fav’,99 );

That’s all.. you can copy this function to your theme’s function.php file, and you have nothing more to do. 🙂

we can transform it as a plugin, so it will be more flexible. you can download it from here