Posted on October 3, 2013 , Last Modified on: October 3, 2013
Show the faces of users who liked a BuddyPress Activity
if( !function_exists('bp_activity_add_meta')):
function bp_activity_add_meta( $activity_id, $meta_key, $meta_value ) {
global $wpdb, $bp;
// Make sure activity_id is valid
if ( !is_numeric( $activity_id ) )
return false;
// Sanitize key
$meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
// Sanitize value
if ( is_string( $meta_value ) )
$meta_value = stripslashes( esc_sql( $meta_value ) );
// Maybe, just maybe... serialize
$meta_value = maybe_serialize( $meta_value );
$wpdb->query( $wpdb->prepare( "INSERT INTO {$bp->activity->table_name_meta} ( activity_id, meta_key, meta_value ) VALUES ( %d, %s, %s )", $activity_id, $meta_key, $meta_value ));
//should we consider invalidating the cache
wp_cache_delete( 'bp_activity_meta_' . $activity_id . '_' . $meta_key, 'bp' );
// Victory is ours!
return true;
}
endif;
add_action( 'bp_activity_add_user_favorite', 'bpdev_track_user_favorite', 10, 2);
function bpdev_track_user_favorite( $activity_id, $user_id ){
bp_activity_add_meta( $activity_id, 'favorited_by_user', $user_id );
}
add_action( 'bp_activity_remove_user_favorite', 'bpdev_track_user_unfavorite', 10, 2);
function bpdev_track_user_unfavorite( $activity_id, $user_id ){
bp_activity_delete_meta( $activity_id, 'favorited_by_user', $user_id );
}
//show faces, yay!
add_action( 'bp_activity_entry_content', 'bpdev_show_who_favorited_activities' );
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>";
}
Put the above code in bp-custom.php
#activity #bp-hacks