Skip to content

Instantly share code, notes, and snippets.

@modemlooper
Created April 17, 2015 14:45
Show Gist options
  • Select an option

  • Save modemlooper/fe67c28b14a15a217eba to your computer and use it in GitHub Desktop.

Select an option

Save modemlooper/fe67c28b14a15a217eba to your computer and use it in GitHub Desktop.
fav post budypress
<?php echo get_fav_or_unfav_button_for_post( $post ); ?>
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment