Skip to content

Instantly share code, notes, and snippets.

@quangbahoa
Created July 8, 2012 22:33
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quangbahoa/3073213 to your computer and use it in GitHub Desktop.
Save quangbahoa/3073213 to your computer and use it in GitHub Desktop.
Buddypress Favorite Post
## functions.php
/*
--------------------------------------------------------------------------------
Favorites button applied to individual posts
Based on: http://www.dnxpert.com/2010/06/11/mark-blog-post-as-favorite-in-buddypress/
--------------------------------------------------------------------------------
*/
function my_bp_activity_is_favorite($activity_id) {
global $bp, $activities_template;
return apply_filters( 'bp_get_activity_is_favorite', in_array( $activity_id, (array)$activities_template->my_favs ) );
}
function my_bp_activity_favorite_link($activity_id) {
global $activities_template;
echo apply_filters( 'bp_get_activity_favorite_link', wp_nonce_url( site_url( BP_ACTIVITY_SLUG . '/favorite/' . $activity_id . '/' ), 'mark_favorite' ) );
}
function my_bp_activity_unfavorite_link($activity_id) {
global $activities_template;
echo apply_filters( 'bp_get_activity_unfavorite_link', wp_nonce_url( site_url( BP_ACTIVITY_SLUG . '/unfavorite/' . $activity_id . '/' ), 'unmark_favorite' ) );
}
## single.php in the loop
<?php global $bp;
$activity_id = bp_activity_get_activity_id( array(
'user_id' => $post->author_id,
'type' => 'new_blog_post',
'component' => 'blogs',
'item_id' => 1,
'secondary_item_id' => $post->ID
) );
?>
<?php if ( is_user_logged_in() ) : ?>
<div role="main" class="activity">
<ul class="post-activity" id="activity-stream">
<li id="activity-<?php echo $activity_id; ?>">
<div>
<div>
<?php bp_has_activities();
if ( !my_bp_activity_is_favorite($activity_id) ) : ?>
<a href="<?php my_bp_activity_favorite_link($activity_id) ?>" class="button bp-secondary-action fav" title="<?php _e( 'Mark as Favorite', 'buddypress' ) ?>"><?php _e( 'Favorite', 'buddypress' ) ?></a>
<?php else : ?>
<a href="<?php my_bp_activity_unfavorite_link($activity_id) ?>" class="button bp-secondary-action unfav" title="<?php _e( 'Remove Favorite', 'buddypress' ) ?>"><?php _e( 'Remove Favorite', 'buddypress' ) ?></a>
<?php endif; ?>
</div>
</div>
</li>
</ul>
</div>
<?php endif;?>
@Magazine3
Copy link

When i click it, it saves as favourite but it shows 'Remove from Fav..' in all posts...

@seanhelmes
Copy link

How can I get rid of the bullet point that shows next to the Favorite link this outputs and how can I get it to not have to refresh the webpage to favorite a post?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment