Skip to content

Instantly share code, notes, and snippets.

@imjjss
Created February 24, 2012 17:37
Show Gist options
  • Save imjjss/1902287 to your computer and use it in GitHub Desktop.
Save imjjss/1902287 to your computer and use it in GitHub Desktop.
add favorit(coped from web, not tested yet)
1. The first thing I did was find out how BuddyPress adds activities in core code when a blog posts is published. I found out that this is done in bp_blogs_record_activity method of bp-blogs.php. Inside this method I found that I can actually retrieve an activity id of a single blog post by calling the method bp_activity_get_activity_id(). If I have the id of an activity, I can easily add it as my favorite from any place on my site.
2. With the above knowledge in hand, I went to my single.php theme template file and added the following inside my the_post loop:
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
) );
As you can see from the code above, I am attempting to retrieve the activity id of the activity ‘new_blog_post’ that was created by the post author and stored against post ID when the blog post was published.
Since I am using BuddyPress with a single WordPress installation, the item_id = 1 reference above is my blog id. If you are planning to use this with WPMU, you’ll have to find your blog id and use that.
3. If you look inside bp-default/activity/entry.php, you will find that an activity entry is added as favorite as follows:
<?php if ( is_user_logged_in() ) : ?>
<?php if ( !bp_get_activity_is_favorite() ) : ?>
<a href="<?php bp_activity_favorite_link() ?>" class="fav" title="<?php _e( 'Mark as Favorite', 'buddypress' ) ?>"><?php _e( 'Favorite', 'buddypress' ) ?></a>
<?php else : ?>
<a href="<?php bp_activity_unfavorite_link() ?>" class="unfav" title="<?php _e( 'Remove Favorite', 'buddypress' ) ?>"><?php _e( 'Remove Favorite', 'buddypress' ) ?></a>
<?php endif; ?>
<?php endif;?>
From the code above, you can see that BP checks that the user is logged in, and whether they have added this activity as favorite already before allowing them to add the activity as Favorite. If they have already added the activity as favorite, an UnFavorite button is provided.
I could not simply reuse the code since the core calls to bp_activity_favorite_link() etc use internal activity ids. Instead, I added the following functions to my functions.php file:
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' ) );
}
Once I had the custom functions in place, I added the following code to my single.php theme file to provide me with a cool “Add as Favorite” button for my blog posts:
<?php if ( is_user_logged_in() ) : ?>
<?php bp_has_activities();
if ( !my_bp_activity_is_favorite($activity_id) ) : ?>
<a href=”<?php my_bp_activity_favorite_link($activity_id) ?>” class=”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=”unfav” title=”<?php _e( ‘Remove Favorite’, ‘buddypress’ ) ?>”><?php _e( ‘Remove Favorite’, ‘buddypress’ ) ?></a>
<?php endif; ?>
<?php endif;?>
@imjjss
Copy link
Author

imjjss commented Feb 24, 2012

why the ajax doesn’t work with BP 1.5 (and so the link sends us on a 404 page)…
The page doesn’t redirect and my ‘favorite’ button changes to ‘remove favorite’ button. But i noticed that it doesn’t save the result. When you refresh your page you can see it didn’t worked/saved. I found in a function it get null value from $_POST['id'].

And again i had spent lot of time, and i put finally same html code (div, ul and li) and then put favorite link inside them. Yeh!! It works!!!

So, my final code are as below

$post->author_id,
‘type’ => ‘new_blog_post’,
‘component’ => ‘blogs’,
‘item_id’ => 1,
‘secondary_item_id’ => $post->ID
) );
?>

  • @imjjss
    Copy link
    Author

    imjjss commented Feb 24, 2012

    So :

    • add $activity_id to the end of the li id to have : li id=”activity-NN” where NN is the activity id
    • after the li, add a div with class=”activity”, and then another div with class=”activity-meta”
    • then, you can have your favorites link,
    • then close the 2 divs and the li (and the ul where you put your li)

    @bureaubold
    Copy link

    Hi Imjjss,
    Thanks for you codings and the time. I just got started with buddypress.
    I just wandered where do you put this $activity_id in, which document.
    And is this one also working for the new buddypress?
    Thanks in advanace,
    Marjet - bureaubold

    @imjjss
    Copy link
    Author

    imjjss commented May 1, 2012

    Hi bureaubold,
    Sorry for not making things clear-- I copied the codes from web including the author's comments on code. I use Gist for collecting codes that I may need but not really understand most of them.
    If you google search the comments, I'm sure you can find the original place for more information.

    @bureaubold
    Copy link

    bureaubold commented May 3, 2012 via email

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