Show favourite count after each activity?
-
I’ve put this into my bp-custom.php:
function my_show_favorite_count() {
$my_fav_count = bp_activity_get_meta( $_POST['id'], 'favorite_count' );
echo $my_fav_count;
}
add_action( 'bp_activity_entry_meta', 'my_show_favorite_count' );to get the total number of times an activity has been favourited…
But, nothing shows up… Am I missing something here?
-
PS: using BP1.2 + WP 2.9.2
Try bp_get_activity_id() in place of $_POST[‘id’].
Yup… that works!
Thanks…
Related question:
How do I show a list (or avatars) or users who’ve favourited the activity?
Here’s how I’m about to attempt to do it:
The first time an activity is favourited, I add a new row to wp_bp_activity_meta with activity_id, a meta_key named my_activity_faved_by, and the user_id as its meta_value.
With every additional favouriting, I update the corresponding meta_value with the new user_id at the end (using CONCAT?).
And in the frontend, I throw out a list of avatars of users who’ve favourited that activity.
Is that the best way about it, or is there something simpler I could be doing?
Look into the bp_core_fetch_avatar() function in bp-core/bp-core-avatars.php.
Sounds good to me. Not sure if there’s a better way performance-wise.
@r-a-y: Thanks, again…
Now, still trying to get the hang of the whole replacing core functions principle…
Will update this once I get some results.
Hit a block here… how do I hook into the function where a user clicks on the favourite button, and make that do something to the database? Using bp_activity_action_mark_favorite? bp_activity_add_user_favorite?
Tried them both, with no results… I know I’m doing something wrong here, but my knowledge of PHP/mySQL is mostly trial and error… (a lot of the latter)
Right now, I’m doing this:
function my_favorite_by_add() {
$my_fav_by_current = bp_activity_get_meta( bp_get_activity_id(), 'favorite_by' );
global $current_user;
get_currentuserinfo();
$my_fav_by_new = $my_fav_by_current . ',' . $current_user->ID ;
bp_activity_update_meta( $activity_id, 'favorite_by', $my_fav_by_new );
}
add_action( 'bp_activity_add_user_favorite', 'my_favorite_by_add' );Any suggestions?
In 1.2.1 I’ve added the missing “bp_activity_remove_user_favorite” and “bp_activity_add_user_favorite” actions that you can hook into. It’ll be out in the next day or two.
Oh.. thanks.
Will get back on this later, then.
Any ideas?
You can plug the code into your theme’s functions.php file.
It doesn’t do much at the moment, as snark is still working on the code and Andy is making refinements to the activity API.
—
Looking forward to your code! This is similar to Hempsworth’s plugin, but I think what you’re working on utilizes what BP already has, which is good.
@snark: I’ve modified the function to this:
function my_favorite_count() {
$my_fav_count = bp_activity_get_meta( bp_get_activity_id(), 'favorite_count' );
if ($my_fav_count >= 1) : {
if (is_user_logged_in()) : {
echo '<span>(' . $my_fav_count . ')</span>';
}
endif;
}
endif;
}
add_action( 'bp_activity_entry_meta', 'my_favorite_count' );Apart from shortening my function name, this shows the count only if the visitor is logged in.
This code goes into bp-custom.php, and it outputs the count next to the Favorite or Remove Favorite button.
@r-a-y: You probably meant this plugin: https://buddypress.org/forums/topic/buddypress-like
You’re right.. it does do pretty much the same thing. But I’ll stick with this, since it’s one of my few attempts at coding something
@Ousep — I don’t have a bp-custom.php file (WPMU? I’m using single-user WP), but I put your function in my child theme functions.php file, and it did something, though it didn’t quite work.
It did not output a count of favorites next to the Favorites button in the Profile sub-nav bar (oddly, such a favorite count does is being displayed in the site-wide Activity stream navigation, however), which is what it should do, but it did output either a “(1)” or a “(2)” just to the right of the “Remove Favorite” button in each of the Favorites listed in Profile –> Activity (Personal) and Profile –> Activity — Favorites. It also posted a “(1)” or a “(2)” just to the right of the “Remove Favorite” button in Activity –> All Members and Activity — Favorites.
Any ideas on this? Thanks.
bp-custom.php goes in the plugins folder… It doesn’t exist, you’ll have to create a php file containing the code above.
<?php
?>And that’s what it does, so far. It shows the number of times each secret has been favourited by users.
[code goes here]
?>
And that's what it does, so far. It shows the number of times each secret has been favourited by users.
Ok, I get it. I added “favorites” to your code, so now it displays “(2) favorites” instead of just “(2)”, to give it a little more meaning for users. Any way to add a conditional line to your function so that if there is one favorite, it will display “(1) favorite” (singular) and if there are two or more, it will display “(x) favorites” (plural)? If this is too nitpicky, or if the favorites plugin in development supercedes all of this, then no worries, I can live without it.
The other “Favorites” display issue I’m trying to solve is to get the Profile sub-nave to display the Favorites count like it does in the activity stream. Right now, they differ in their displays, like this:
On Activity Stream: Favorites (2)
On Profile Sub-nav: Favorites
Thanks!
I got all excited about this until I realized this was for 1.2 not 1.1.3
@snark: Try this, for pluralization…
function my_favorite_count() {
$my_fav_count = bp_activity_get_meta( bp_get_activity_id(), 'favorite_count' );
if ($my_fav_count >= 1) : {
if (is_user_logged_in()) : {
echo '<span>(' . $my_fav_count . ')</span> favorite';
if ($my_fav_count > 1) :
{echo 's';} //this turns favorite into favorites
endif;
}
endif;
}
endif;
}
add_action( 'bp_activity_entry_meta', 'my_favorite_count' );I’ve been sidetracked by a couple of other things that needed my attention, so I haven’t been able to make it do what I’d really like it to do, ie: show the number within the favorite link, like it shows in the reply link:
[Reply (12)] [Favorite (23)]
.For consistency.
And the other issue of updating the number on clicking the link.
And, showing the users who’ve favourited the activity.
About the other issue, I’ve been trying to find the source of those links, too… Not much luck with that, yet.
@ousep: Thanks for all your work on this, I really appreciate it. The new tweak works great, but if you ever get around to getting the count inline — [Favorite (23)] — I’ll be looking forward to it here.
Hi, where exactly did you put the code you posted at first & on which file?
@liesl1698 paste it in your theme’s functions.php
Anyone managed to get what @snark said? Getting the count inline — [Favorite (23)]
This would be a really nice feature for BP users.Cheers
- The topic ‘Show favourite count after each activity?’ is closed to new replies.