Which interface are you meaning ? You already have a My favourite tab when you’re logged in and on the SWA or on your profile.
To add a favorite/unfavorite button under a blog post, add this snippet to bp-custom.php
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="'.__( 'Ajouter à mes favoris', 'buddypress' ).'">'.__( 'Favori', 'buddypress' ).'</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="'.__( 'Retirer de mes favoris', 'buddypress' ).'">'.__( 'Défavoriser', 'buddypress' ).'</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">'.__( 'Consulter tous mes favoris', 'buddypress' ).'</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;
}
and this piece of code in your child-theme single.php file. Where exactly depends of the theme you use, but logically near the post, inside div#content
<?php echo get_fav_or_unfav_button_for_post( $post ); ?>
Hey dan i tried this code and the button doesnt seem to show up in my cpt’s
@chrisbenge did you find a solution to your q? I’m looking to do the same…
Also looking for this… it seems to save somewhere because if you close tab and reopen it remembers if you favorited the post.. Just not showing up in the favorites page!
Help!
Ahh I see another issue.. Its not actually favoriting the specific post. If I click the favorite button on one post, and go to another post, it will show the unfavorite button. Same with any post, its globally effecting the state of the button..