Favorite blog post (working with ajax?)
-
Hello!
For the site i’m currently working on, it’s important that the users are allowed to favorite blog posts and that it appears on their “favorite” tab on profile. I’m using WordPress 4.0.1 / BuddyPress 2.1.1.
The following code does that, but is there a way to make it work like the ajaxed favorite buttons on the activity page instead of reloading the page so the favorite option will be saved?
Thanks so much!
Here’s the code i have on functions.php
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' ) );
}
Code i have on index.php / single.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() ) : ?>
<?php bp_has_activities();
if ( !my_bp_activity_is_favorite($activity_id) ) : ?>
<li class="activity-meta">
[a href="<?php my_bp_activity_favorite_link($activity_id) ?>" class="button fav bp-secondary-action"]Favorite[/a]
<?php else : ?>
[a href="<?php my_bp_activity_unfavorite_link($activity_id) ?>" class="unfav button bp-secondary-action"]Unfavorite[/a]<br>
[a href="<?php echo bp_loggedin_user_domain() . BP_ACTIVITY_SLUG . '/favorites/' ?>" style="font-size: 12px;"]View Your Favorites[/a]
<?php endif; ?>
<?php endif;?>
a tags were replaced by [a] so the code will appear above.
- The topic ‘Favorite blog post (working with ajax?)’ is closed to new replies.