Skip to:
Content
Pages
Categories
Search
Top
Bottom

How do I add "Favorites" to activity comments?

  • Avatar of godavid33
    godavid33
    Participant

    @godavid33

    BP 2.0.1 WP 3.9.1

    Theoretically it should be as simple as adding the favorite code right? But it’s not apparently… I even went as far to go upstream and directly echo the filtered links for favoriting:

    
    apply_filters( 'bp_get_activity_favorite_link', wp_nonce_url( home_url( bp_get_activity_root_slug() . '/favorite/' . bp_get_activity_comment_id() . '/' ), 'mark_favorite' ) )
    

    But I’m still having trouble. I click the favorite button and nothing happens. Shouldnt comments be treated the same as normal activities?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Avatar of godavid33
    godavid33
    Participant

    @godavid33

    Bump?

    Avatar of godavid33
    godavid33
    Participant

    @godavid33

    Ok, well I had to figure this one out. It’s a bit of a lengthy solution. I’m not going to explain everything, as I can’t for either lack of knowledge or fear of being incorrect. I’ll just give you the code, tell you what it does, and where it goes.

    First, lets add the favorite button to the comments template. Find comment.php (in /activity) and add the code:

    
    		<?php global $activities_template; ?>
    		<?php if ( bp_activity_can_favorite() ) : ?>
    			<?php
    				$my_fav_count = bp_activity_get_meta( bp_get_activity_comment_id(), 'favorite_count' );
    				
    				$my_fav_count = "<span>".$my_fav_count."</span>";
    				
    				$is_favorite = apply_filters( 'bp_get_activity_is_favorite', in_array( bp_get_activity_comment_id(), $activities_template->my_favs ) );
    			?>
    
    			<?php if ( !$is_favorite ) : ?>
    
    				<a href="<?php echo apply_filters( 'bp_get_activity_favorite_link', wp_nonce_url( home_url( bp_get_activity_root_slug() . '/favorite/' . bp_get_activity_comment_id() . '/' ), 'mark_favorite' ) ); ?>" class="tooltip-trigger fav bp-secondary-action bp-primary-action" title="<?php echo god_get_favorited_usernames(bp_get_activity_comment_id()); ?>" style="position:relative;"><?php _e( 'Like', 'buddypress' ); ?><?php echo $my_fav_count; ?></a>
    
    			<?php else : ?>
    
    				<a href="<?php echo apply_filters( 'bp_get_activity_favorite_link', wp_nonce_url( home_url( bp_get_activity_root_slug() . '/favorite/' . bp_get_activity_comment_id() . '/' ), 'mark_favorite' ) ); ?>" class="tooltip-trigger unfav bp-secondary-action bp-primary-action" title="<?php echo god_get_favorited_usernames(bp_get_activity_comment_id()); ?>" style="position:relative;"><?php _e( 'Unlike', 'buddypress' ); ?><?php echo $my_fav_count; ?></a>
    
    			<?php endif; ?>
    
    		<?php endif; ?>	
    

    This code simply adds the buttons, and gets all the necessary info and variables.

    Then, you’re going to need to modify the buddypress javascript. I personally modified both global.js and buddypress.js (through overriding in my template, which is a topic for a whole other thread), though I might be wrong in this. At any rate, look for the line (around 264 in buddypress.js):

    
    var parent = target.closest('.activity-item');
    

    and change it to

    
    var parent = target.closest('li');
    

    The reason we changed this line is so that our custom favorite button doesn’t default to grabbing the top level activity id. Now you can actually favorite comments!

    But wouldn’t it be nice if the user received a notification when someone favorited their activity? Add this bad boy to functions.php

    
    
    add_action("bp_activity_add_user_favorite","favorite_notification", 10, 2);
    
    function favorite_notification( $activity_id, $user_id = 0){
    	// By type and item_id
    	if($user_id != $activities["activities"][0]->user_id){
    		$activities = bp_activity_get_specific( array( 'activity_ids' => $activity_id, 'display_comments' => true) );
    		
    		bp_core_add_notification( $activity_id, $activities["activities"][0]->user_id, "notifier", bp_core_get_username(bp_loggedin_user_id())." liked: '".substr($activities["activities"][0]->content,0,20)."...'", $activity_id ) ;		
    	}
    }
    

    Hope this helped someone. It mostly works for me.

Viewing 2 replies - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.