Skip to:
Content
Pages
Categories
Search
Top
Bottom

Reply To: Help with custom bbpress Like/Dislike plugin

@mth75

Participant

The trick is to relate the activity ID to the Post ID of the forum topic or reply. This code (used for liking (aka favouriting) ) blogpost works. I’m trying to rewrite it for bbpress

In single.php:

<?php echo get_fav_or_unfav_button_for_post( $post ); ?>

In function.php:

// Buddypress favorites
function get_fav_or_unfav_button_for_post( $post ) {
global $bp, $activities_template;
        // user is not logged ? Show nothing.
	if ( ! is_user_logged_in() ) {
	return '';
	}
	
	$activity_id = bp_activity_get_activity_id( array(
		'user_id' => $post->post_author,
		'type' => 'new_blog_post',
		'component' => 'blogs',
		'item_id' => 1,
		'secondary_item_id' => $post->ID
		) );
	
	if ( ! $activity_id ) {
	return '';
	}
	
		bp_has_activities(); // update $activities_template of user's fav
		$old_value = false;
		
		if ( isset( $activities_template->activity->id ) ) {
		$old_value = $activities_template->activity->id;
		$activities_template->activity->id = $activity_id;
		} else {
		$activities_template->activity = (object) array( 'id' => $activity_id );
		}
	
		// building the template
	$code = '';
	$code .= '<div class="activity-meta">'."\n";

		if ( ! bp_get_activity_is_favorite() ) {
		// if not favorited, add a fav button
		$code .= ' <a href="'.bp_get_activity_favorite_link( ).'" class="button fav bp-secondary-action" title="'.__( 'Like', 'buddypress' ).'">'.__( 'Like', 'buddyboss' ).'</a>'."\n";
		
		} else {
		
		// if already favorited, a button to unfav
		$code .= ' <a href="'.bp_get_activity_unfavorite_link( ).'" class="button unfav bp-secondary-action" title="'.__( 'Unlike', 'buddypress' ).'">'.__( 'Unlike', 'buddyboss' ).'</a>'."\n";
		
		// bonus button to show user's all favs
		$code .= ' <a href="'.bp_loggedin_user_domain() . 'activity/favorites/" class="button unfav bp-secondary-action">'.__( 'My Likes', 'buddyboss' ).'</a>'."\n";
		}
		
		// closing .activity-meta
	$code .= '</div>'."\n"; 

		if ( false !== $old_value ) {
		$activities_template->activity->id = $old_value;
		} else {
		$activities_template->activity = null;
		}
	return $code;
}
Skip to toolbar