Adding Topics View Counter in BBPress
BBPress is a WordPress Plugin alternative to other Forum Software such as vBulletin, phpBB, Xenforo, and Vanilla. Although BBPress did not have as many features as vBulletin and Xenforo, it had a higher Search Engine Optimize score than both of them plus BBPress is Free to use. BBPress uses WordPress Permalink which is Better SEO Friendly, many webmasters are taken interest in using BBPress as their Community Forum Tool.
Today I am going to show you how to add a view counter to BBPress topics.
First, make sure you have BBPress plugin installed, you can download the BBPress WordPress Plugin here.
1. Adding View Counter Function
open your theme functions.php and add this line of code
[php]
if( !function_exists(‘get_wpbbp_post_view’) ) :
////////////////////////////////////////////////////////////////////////////////
// get bbpress topic view counter
////////////////////////////////////////////////////////////////////////////////
function get_wpbbp_post_view($postID){
$count_key = ‘post_views_count’;
$count = get_post_meta($postID, $count_key, true);
if($count==”){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, ‘0’);
return "0";
}
return $count;
}
function set_wpbbp_post_view($postID) {
$count_key = ‘post_views_count’;
$count = get_post_meta($postID, $count_key, true);
if( $count == ” ){
add_post_meta($postID, $count_key, ‘1’);
} else {
$new_count = $count + 1;
update_post_meta($postID, $count_key, $new_count);
}
}
endif;
[/php]
What does this code do?
this is the function to add view counter to topic meta
2. Add Hook Action to Each Topic
Again, below the newly added code, add this line of code
[php]
if( !function_exists(‘add_wpbbp_topic_counter’) ) :
////////////////////////////////////////////////////////////////////////////////
// init the view counter in bbpress single topic
////////////////////////////////////////////////////////////////////////////////
function add_wpbbp_topic_counter() {
global $wp_query;
$bbp = bbpress();
$active_topic = $bbp->current_topic_id;
set_wpbbp_post_view( $active_topic );
}
add_action(‘bbp_template_after_single_topic’, ‘add_wpbbp_topic_counter’);
endif;
[/php]
3. Applying or use the counter in template
You can use this code in any bbpress loop
[php]<?php echo get_wpbbp_post_view( bbp_get_topic_id() ); ?>[/php]
in my case, i want to replace the ‘voice count’ with ‘view count’ in the BBPress topics loop.
using a bbpress child template method, I created a bbpress folder name in my theme. Copy and paste loop-single-topic.php from [highlight color=”” bgcolor=”#eee”]wp-content/plugins/bbpress/templates/default/bbpress/loop-single-topic.php[/highlight]
then edit [highlight color=”” bgcolor=”#eee”]mytheme/bbpress/loop-single-topic.php[/highlight] line 82+
[php]<?php bbp_topic_voice_count(); ?>[/php]
with the new code
[php]<?php echo get_wpbbp_post_view( bbp_get_topic_id() ); ?>[/php]
The voice counter should have been replacing by the view counter right now.
Good to go now!
Once everything is done, you should have a view counter on all your BBPress topics. Now you can track which topics had the most viewed starting today!
Hope you enjoy the read, more BBPress Tips, and Tricks coming this month.
interesting
Thanks for the tutorial. We’ve been wanting to do on our forum and your simple-to-implement method works flawlessly. We look forward to your upcoming bbpress tips & tricks.
Dawn
Thank you for this tutorial! Just what I needed! Funny how BBPRess doesn’t have this piece of functionality out of the box..
thanks, i have applied for my website and it is bring good affect
Hello & thank you for this fantastic code to add a view counter. I do have one question. I have some threads that have 4 digit view counts, how can I get it to add a comma? Like 1,024 instead of 1024?
Thanks
Nevermind, I got it!
add number_format before the return $count
like
return number_format($count);
Hi, I’m Hong.
I don’t know ‘Add Hook Action to Each Topics’ and ‘Applying or use the counter in template’.
How to do I?… Could you comment detail instruction ?
I am a begginer of php(wordpress).
hi,
i like what you did, but is it possible that i can put a limit to the view count? i min i have 5 members inside a group forum. so the view count would only be 5 also. please help.
yes just store the value in a variable and if its over 5 output 5 instead
note: if one person views it 5 times each visit is counted so probably not what your looking for
Thanks, this is what I have been looking for… One question, though. My column header still says Voices, how do I change it to something more appropriate?
You Can change it in /loop-topics.php > change “Voices” to “Views”.
Thanks, for good for me, just add some (int) in the PHP to be sure the count is good 😉
Thanks for this awesome article.
It’s work for me 🙂
Thanks for this tutorial, i have i can do’it
Hi,
Thanks for the code. It works fine and I am able to display the View count. Do I need to make any changes to the css to neaten up the UI. Now the counts for all the columns seemed to have moved to the left side.
Please suggest