Añadir follow/unfollow en activity stream-Buddypress

En este tutorial veremos como añadir una entrada en el activity stream de Buddypress cada vez que un usuario sigue o deja de seguir a otro usando el plugin Buddypress Follw

En functions.php de nuestro tema activo añadimos las siguientes líneas de código

/* Add a new activity stream item for when a user start following somebody */
function inauditas_start_following_activity() {
    global $bp;
    if ( !function_exists( 'bp_activity_add' ) )
    return false;
    $user_following = bp_loggedin_user_id();
    $user_following_link =  bp_core_get_userlink( $user_following );
    $user_followed_id =$_POST['uid'];//we use this method otherwise it doesn't work if somebody use the follow button from the all members page instead of the single user page
    $user_firstName=get_user_meta($user_followed,'first_name',true);
    $user_lastName=get_user_meta($user_followed,'last_name',true);
    $user_nicename=bp_core_get_username($user_followed_id);
	
    $userlink = bp_core_get_userlink( $user_followed_id );

    bp_activity_add( array(
	'user_id' => $user_following,
	'action' => sprintf( __( '%1$s started following %2$s', 'buddypress' ), $user_following_link, $userlink), 
	'component' => 'activity',
	'type' => 'new_following',
	'secondary_item_id' => $user_followed_id
	) );

}
add_action( 'bp_follow_start_following', 'inauditas_start_following_activity' );

/* Add a new activity stream item for when a user stop following somebody */
function inauditas_stop_follow_activity() {
    global $bp;
    if ( !function_exists( 'bp_activity_add' ) )
    return false;
    $user_following = bp_loggedin_user_id();
    $user_following_link =  bp_core_get_userlink( $user_following );
    $user_followed_id =$_POST['uid'];
    $user_firstName=get_user_meta($user_followed,'first_name',true);
    $user_lastName=get_user_meta($user_followed,'last_name',true);
    $user_nicename=bp_core_get_username($user_followed_id);
    
    $userlink = bp_core_get_userlink( $user_followed_id );

    bp_activity_add( array(
	'user_id' => $user_following,
	'action' =>  sprintf( __( '%1$s stopped following %2$s', 'buddypress' ), $user_following_link, $userlink), 
	'component' => 'activity',
	'type' => 'stop_following',
	'secondary_item_id' => $user_followed_id
    ) );

}
add_action( 'bp_follow_stop_following', 'inauditas_stop_follow_activity' );

En al archivo activity-loop.php del Buddypress tendremos que añadir al loop las entradas que acabamos de crear

<?php 
global $wpdb;
global $bp;
$displayed_user=$bp->displayed_user->id;
$activity_ids= array();?>
//We retrieve entries in which we can be either the the follower or the followed user
<?php $activity_ids = $wpdb->get_results( "SELECT id FROM {$bp->activity->table_name} WHERE (user_id LIKE $displayed_user OR secondary_item_id LIKE $displayed_user) AND (type LIKE 'stop_following' OR type LIKE 'new_following')" );

foreach ( $activity_ids as $activity_id ) {
    $query_aids[] = $activity_id->id;
}?>
<?php 
$activity_user_ids = implode( ',', array_unique( (array) $query_aids ) );
$args=array(
        'include' => $activity_user_ids,  
    );
?>

<?php if ( bp_has_activities( $args) ) : ?>

//......the loop starts here

Es aconsejable crear la carpeta /wp-content/themes/my_theme/buddypress/activity en la que copiamos todos los archivos de buddypress que queremos modificar para no perder los cambios en caso de actualizar.

Deja un comentario